使用Java.util.zip下的zipOutStream和zipInStream对字节流进行压缩和解压缩

参考:http://www.blogjava.net/usherlight/archive/2007/09/26/148230.html

 

 

网上介绍使用zipInStream和zipOutStream对文件或者文件夹进行压缩和解压缩的文章比较多。
但是这次项目中需要对byte[]进行压缩,然后decode,通过http发送到服务端。

最简单的方法,当然是把byte[]写到文件里,然后根据网上已有的文章,生成fileInputStream,构造zipInStream。
但是这个做法有着明显的问题,需要操作IO,在效率上不可取。

下面是利用ByteArrayOutStream来完成压缩和解压缩的代码。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->

   
/**
     * 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();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值