<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    云自無心水自閑

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

    網上介紹使用zipInStream和zipOutStream對文件或者文件夾進行壓縮和解壓縮的文章比較多。
    但是這次項目中需要對byte[]進行壓縮,然后decode,通過http發送到服務端。

    最簡單的方法,當然是把byte[]寫到文件里,然后根據網上已有的文章,生成fileInputStream,構造zipInStream。
    但是這個做法有著明顯的問題,需要操作IO,在效率上不可取。

    下面是利用ByteArrayOutStream來完成壓縮和解壓縮的代碼。



       
    /**
         * 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();
        }



    評論

    # re: 使用Java.util.zip下的zipOutStream和zipInStream對字節流進行壓縮和解壓縮  回復  更多評論   

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

    # re: 使用Java.util.zip下的zipOutStream和zipInStream對字節流進行壓縮和解壓縮  回復  更多評論   

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

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 两性色午夜免费视频| 国产免费的野战视频| 午夜亚洲AV日韩AV无码大全| 日韩免费无码一区二区三区| 亚洲综合无码一区二区痴汉| 亚洲一区精品伊人久久伊人 | 少妇人妻偷人精品免费视频| 亚洲日韩精品国产3区| 亚洲日韩精品无码专区网站| 67pao强力打造高清免费| 精品亚洲国产成人av| 亚洲国产第一站精品蜜芽| 成年女人毛片免费视频| a色毛片免费视频| 亚洲高清中文字幕免费| 亚洲AV无码国产在丝袜线观看| 免费视频淫片aa毛片| 午夜无码A级毛片免费视频| 国产午夜亚洲精品不卡电影| 亚洲视频在线一区| 亚洲午夜av影院| 成人毛片手机版免费看| 一级毛片免费观看| 一级毛片免费播放男男| 亚洲乱码在线卡一卡二卡新区| 亚洲精品无码MV在线观看 | 无码欧精品亚洲日韩一区夜夜嗨 | 亚洲伊人色欲综合网| 在线免费观看a级片| 91免费国产精品| 成全动漫视频在线观看免费高清版下载| 亚洲国产成人精品无码一区二区 | 国产精品视频免费观看| 波霸在线精品视频免费观看| 亚洲av无码一区二区三区天堂 | 亚洲一级毛片免费观看| 国产亚洲欧洲Aⅴ综合一区| 国产美女精品视频免费观看| 免费视频专区一国产盗摄| 无码专区AAAAAA免费视频| 在线观看人成视频免费无遮挡|