云自無(wú)心水自閑

          天平山上白云泉,云自無(wú)心水自閑。何必奔沖山下去,更添波浪向人間!
          posts - 288, comments - 524, trackbacks - 0, articles - 6
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          網(wǎng)上介紹使用zipInStream和zipOutStream對(duì)文件或者文件夾進(jìn)行壓縮和解壓縮的文章比較多。
          但是這次項(xiàng)目中需要對(duì)byte[]進(jìn)行壓縮,然后decode,通過http發(fā)送到服務(wù)端。

          最簡(jiǎn)單的方法,當(dāng)然是把byte[]寫到文件里,然后根據(jù)網(wǎng)上已有的文章,生成fileInputStream,構(gòu)造zipInStream。
          但是這個(gè)做法有著明顯的問題,需要操作IO,在效率上不可取。

          下面是利用ByteArrayOutStream來(lái)完成壓縮和解壓縮的代碼。



             
          /**
               * Answer a byte array compressed in the Zip format from bytes.
               * 
               * 
          @param bytes
               *            a byte array
               * 
          @param aName
               *            a String the represents a file name
               * 
          @return byte[] compressed bytes
               * 
          @throws IOException
               
          */
              
          public static byte[] zipBytes(byte[] bytes) throws IOException {
                  ByteArrayOutputStream tempOStream 
          = null;
                  BufferedOutputStream tempBOStream 
          = null;
                  ZipOutputStream tempZStream 
          = null;
                  ZipEntry tempEntry 
          = null;
                  
          byte[] tempBytes = null;

                  tempOStream 
          = new ByteArrayOutputStream(bytes.length);
                  tempBOStream 
          = new BufferedOutputStream(tempOStream);
                  tempZStream 
          = new ZipOutputStream(tempBOStream);
                  tempEntry 
          = new ZipEntry(String.valueOf(bytes.length));
                  tempEntry.setMethod(ZipEntry.DEFLATED);
                  tempEntry.setSize((
          long) bytes.length);
                  
                  tempZStream.putNextEntry(tempEntry);
                  tempZStream.write(bytes, 
          0, bytes.length);
                  tempZStream.flush();
                  tempBOStream.flush();
                  tempOStream.flush();
                  tempZStream.close();
                  tempBytes 
          = tempOStream.toByteArray();
                  tempOStream.close();
                  tempBOStream.close();
                  
          return tempBytes;
              }


              
          /**
               * Answer a byte array that has been decompressed from the Zip format.
               * 
               * 
          @param bytes
               *            a byte array of compressed bytes
               * 
          @return byte[] uncompressed bytes
               * 
          @throws IOException
               
          */
              
          public static void unzipBytes(byte[] bytes, OutputStream os) throws IOException {
                  ByteArrayInputStream tempIStream 
          = null;
                  BufferedInputStream tempBIStream 
          = null;
                  ZipInputStream tempZIStream 
          = null;
                  ZipEntry tempEntry 
          = null;
                  
          long tempDecompressedSize = -1;
                  
          byte[] tempUncompressedBuf = null;

                  tempIStream 
          = new ByteArrayInputStream(bytes, 0, bytes.length);
                  tempBIStream 
          = new BufferedInputStream(tempIStream);
                  tempZIStream 
          = new ZipInputStream(tempBIStream);
                  tempEntry 
          = tempZIStream.getNextEntry();
                  
                  
          if (tempEntry != null) {
                      tempDecompressedSize 
          = tempEntry.getCompressedSize();
                      
          if (tempDecompressedSize < 0) {
                          tempDecompressedSize 
          = Long.parseLong(tempEntry.getName());
                      }

                      
          int size = (int)tempDecompressedSize;
                      tempUncompressedBuf 
          = new byte[size];
                      
          int num = 0, count = 0;
                      
          while ( true ) {
                          count 
          = tempZIStream.read(tempUncompressedBuf, 0, size - num );
                          num 
          += count;
                          os.write( tempUncompressedBuf, 
          0, count );
                          os.flush();
                          
          if ( num >= size ) break;
                      }
                  }
                  tempZIStream.close();
              }



          評(píng)論

          # re: 使用Java.util.zip下的zipOutStream和zipInStream對(duì)字節(jié)流進(jìn)行壓縮和解壓縮  回復(fù)  更多評(píng)論   

          2007-09-27 19:20 by 千里冰封
          有些ZIP文件是不能解的

          # re: 使用Java.util.zip下的zipOutStream和zipInStream對(duì)字節(jié)流進(jìn)行壓縮和解壓縮  回復(fù)  更多評(píng)論   

          2007-09-27 21:06 by 云自無(wú)心水自閑
          這里提供的是壓縮和解壓縮的對(duì)稱方案啊。

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 宁国市| 长顺县| 白水县| 通辽市| 肃北| 突泉县| 额尔古纳市| 合水县| 习水县| 资兴市| 栾城县| 苏尼特左旗| 闻喜县| 崇信县| 武定县| 浦江县| 南宫市| 二手房| 扶绥县| 保康县| 冷水江市| 隆化县| 托克托县| 鄯善县| 天峻县| 乐亭县| 左贡县| 周宁县| 新巴尔虎右旗| 耿马| 武冈市| 光泽县| 静安区| 滦南县| 稷山县| 盐城市| 沈丘县| 古交市| 和硕县| 天祝| 公主岭市|