<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帛羅閣 閱讀(1362) 評論(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)

    隨筆分類

    隨筆檔案

    相冊

    收藏夾

    博客

    文檔

    站點

    論壇

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲欧洲国产综合| 亚洲国产成人久久一区WWW| 亚洲高清专区日韩精品| 一区二区在线视频免费观看| 天堂亚洲免费视频| 免费看黄福利app导航看一下黄色录像| 久久精品女人天堂AV免费观看| 91午夜精品亚洲一区二区三区| 亚州免费一级毛片| 亚洲午夜电影在线观看高清| 成人无码区免费A片视频WWW| 中文字幕在线观看亚洲视频| 在线免费观看一级片| 亚洲欧洲日产国码久在线| 国产乱子影视频上线免费观看| 色婷婷亚洲一区二区三区| 免费人成网站7777视频| 国产美女视频免费观看的网站| 亚洲日韩欧洲无码av夜夜摸| 日韩电影免费在线观看| 亚洲狠狠狠一区二区三区| 国内免费高清在线观看| 国产精品无码亚洲精品2021| MM131亚洲国产美女久久| 免费一级毛片无毒不卡| 亚洲午夜电影在线观看| 国产免费爽爽视频免费可以看| 国产成人精品免费大全| 无码乱人伦一区二区亚洲| 青娱乐免费视频在线观看| 国产偷国产偷亚洲高清人| 亚洲av无码一区二区三区不卡| 久草免费在线观看视频| 偷自拍亚洲视频在线观看| 亚洲av无码一区二区三区网站 | 成人在线免费观看| 九一在线完整视频免费观看| 亚洲午夜精品一区二区| 成人爱做日本视频免费| 人妻在线日韩免费视频| 亚洲中文字幕久久久一区|