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

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

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

    Dev@Free

    zJun's Tech Weblog

    Compress and Uncompress in Java

    整理自: The Java Developers Almanac 1.4

    Compressing a File in the GZIP Format:
    try ?{
    ????
    // ?Create?the?GZIP?output?stream
    ????String?outFilename? = ? " outfile.gzip " ;
    ????GZIPOutputStream?out?
    = ? new ?GZIPOutputStream( new ?FileOutputStream(outFilename));

    ????
    // ?Open?the?input?file
    ????String?inFilename? = ? " infilename " ;
    ????FileInputStream?in?
    = ? new ?FileInputStream(inFilename);

    ????
    // ?Transfer?bytes?from?the?input?file?to?the?GZIP?output?stream
    ???? byte []?buf? = ? new ? byte [ 1024 ];
    ????
    int ?len;
    ????
    while ?((len? = ?in.read(buf))? > ? 0 )?{
    ????????out.write(buf,?
    0 ,?len);
    ????}
    ????in.close();

    ????
    // ?Complete?the?GZIP?file
    ????out.finish();
    ????out.close();
    }?
    catch ?(IOException?e)?{
    }

    Uncompressing a File in the GZIP Format:
    try ?{
    ????
    // ?Open?the?compressed?file
    ????String?inFilename? = ? " infile.gzip " ;
    ????GZIPInputStream?in?
    = ? new ?GZIPInputStream( new ?FileInputStream(inFilename));

    ????
    // ?Open?the?output?file
    ????String?outFilename? = ? " outfile " ;
    ????OutputStream?out?
    = ? new ?FileOutputStream(outFilename);

    ????
    // ?Transfer?bytes?from?the?compressed?file?to?the?output?file
    ???? byte []?buf? = ? new ? byte [ 1024 ];
    ????
    int ?len;
    ????
    while ?((len? = ?in.read(buf))? > ? 0 )?{
    ????????out.write(buf,?
    0 ,?len);
    ????}

    ????
    // ?Close?the?file?and?stream
    ????in.close();
    ????out.close();
    }?
    catch ?(IOException?e)?{
    }

    Creating a ZIP File:
    // ?These?are?the?files?to?include?in?the?ZIP?file
    String[]?filenames? = ? new ?String[]{ " filename1 " ,? " filename2 " };

    // ?Create?a?buffer?for?reading?the?files
    byte []?buf? = ? new ? byte [ 1024 ];

    try ?{
    ????
    // ?Create?the?ZIP?file
    ????String?outFilename? = ? " outfile.zip " ;
    ????ZipOutputStream?out?
    = ? new ?ZipOutputStream( new ?FileOutputStream(outFilename));

    ????
    // ?Compress?the?files
    ???? for ?( int ?i = 0 ;?i < filenames.length;?i ++ )?{
    ????????FileInputStream?in?
    = ? new ?FileInputStream(filenames[i]);

    ????????
    // ?Add?ZIP?entry?to?output?stream.
    ????????out.putNextEntry( new ?ZipEntry(filenames[i]));

    ????????
    // ?Transfer?bytes?from?the?file?to?the?ZIP?file
    ???????? int ?len;
    ????????
    while ?((len? = ?in.read(buf))? > ? 0 )?{
    ????????????out.write(buf,?
    0 ,?len);
    ????????}

    ????????
    // ?Complete?the?entry
    ????????out.closeEntry();
    ????????in.close();
    ????}

    ????
    // ?Complete?the?ZIP?file
    ????out.close();
    }?
    catch ?(IOException?e)?{
    }

    Listing the Contents of a ZIP File:
    try ?{
    ????
    // ?Open?the?ZIP?file
    ????ZipFile?zf? = ? new ?ZipFile( " filename.zip " );

    ????
    // ?Enumerate?each?entry
    ???? for ?(Enumeration?entries? = ?zf.entries();?entries.hasMoreElements();)?{
    ????????
    // ?Get?the?entry?name
    ????????String?zipEntryName? = ?((ZipEntry)entries.nextElement()).getName();
    ????}
    }?
    catch ?(IOException?e)?{
    }

    Retrieving a Compressed File from a ZIP File:
    try ?{
    ????
    // ?Open?the?ZIP?file
    ????String?inFilename? = ? " infile.zip " ;
    ????ZipInputStream?in?
    = ? new ?ZipInputStream( new ?FileInputStream(inFilename));

    ????
    // ?Get?the?first?entry
    ????ZipEntry?entry? = ?in.getNextEntry();

    ????
    // ?Open?the?output?file
    ????String?outFilename? = ? " o " ;
    ????OutputStream?out?
    = ? new ?FileOutputStream(outFilename);

    ????
    // ?Transfer?bytes?from?the?ZIP?file?to?the?output?file
    ???? byte []?buf? = ? new ? byte [ 1024 ];
    ????
    int ?len;
    ????
    while ?((len? = ?in.read(buf))? > ? 0 )?{
    ????????out.write(buf,?
    0 ,?len);
    ????}

    ????
    // ?Close?the?streams
    ????out.close();
    ????in.close();
    }?
    catch ?(IOException?e)?{
    }

    posted on 2008-06-03 23:02 zJun's帛羅閣 閱讀(1363) 評論(1)  編輯  收藏 所屬分類: Java基礎

    評論

    # re: Compress and Uncompress in Java 2009-12-20 05:54 Krista22

    The whole internet is full with <a href="http://www.primedissertations.com">thesis writing service</a> and thesis proposal referring to this good topic though to get really good accomplished <a href="http://www.primedissertations.com">dissertation</a>, students can find it.   回復  更多評論   

    導航

    <2008年6月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    統計

    常用鏈接

    留言簿(15)

    隨筆分類

    隨筆檔案

    相冊

    收藏夾

    博客

    文檔

    站點

    論壇

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 精品国产日韩亚洲一区| 国产成人aaa在线视频免费观看 | 免费人成网站在线高清| 亚洲高清有码中文字| A在线观看免费网站大全| 亚洲国产成人超福利久久精品 | 无码区日韩特区永久免费系列| 亚洲国产国产综合一区首页| 久草免费福利视频| 色播亚洲视频在线观看| 999久久久免费精品播放| 亚洲高清免费在线观看| 18未年禁止免费观看| 亚洲精品456在线播放| av无码免费一区二区三区| 亚洲a级片在线观看| 最近中文字幕免费mv视频7| 亚洲av日韩av永久在线观看| 国产中文字幕免费观看| fc2免费人成在线| 久久精品国产精品亚洲艾草网| 久久久久久AV无码免费网站| 亚洲狠狠狠一区二区三区| 最近2019中文字幕免费看最新| 亚洲日韩AV一区二区三区中文| 免费久久精品国产片香蕉| 国产精品免费久久久久电影网| 亚洲AV日韩AV天堂一区二区三区 | 亚洲成在人线在线播放无码| 四虎影视在线永久免费观看| 成人无码区免费A∨直播| 精品亚洲成a人片在线观看| 成年女人18级毛片毛片免费观看| 日韩色日韩视频亚洲网站| 国产亚洲美女精品久久久2020| 一区二区在线免费观看| 亚洲日本成本人观看| 亚洲精品无码专区在线在线播放 | 亚洲国产模特在线播放| 免费涩涩在线视频网| 中文字幕在线免费视频|