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

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

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

    posts - 241,  comments - 116,  trackbacks - 0
    示例用的是org.apache.tools.zip包下面的一些類,jdk自帶的zip工具類當文件名是中文的情況下會出現問題,本事例可以遞歸壓縮文件和解壓文件,功能上和現在常用的一些壓縮軟件功能類似,暫時沒有做性能上的對比。
    package zip;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Enumeration;

    import org.apache.tools.zip.ZipEntry;
    import org.apache.tools.zip.ZipFile;
    import org.apache.tools.zip.ZipOutputStream; javascript table 表格排序



    @SuppressWarnings("rawtypes")
    public class TestAntZip {
        public static void main(String[] args) {
            //Unzip();
            //ZipFile();
        }

        /**
         * 壓縮文件或者文件夾
         */
        private static void ZipFile() {
            try {
                String inputStr = "D:/testzip";
                File input = new File(inputStr);
                            String singleFile = null;
                if (input.isFile()) {
                  int temp = input.getName().lastIndexOf(".");
                      singleFile = input.getName().substring(0, temp);
                }
                String basepath = input.getName();
                            String zipfilename = singlefile != null ? singlefile : input.getName();
                ZipOutputStream zos = new ZipOutputStream(new File(input.getParent(), zipfilename + ".zip"));
                zos.setEncoding("UTF-8");
                PutEntry(zos, input, basepath);
                zos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        private static void PutEntry(ZipOutputStream zos, File file, String basepath) throws IOException, FileNotFoundException {
            FileInputStream is = null;
            byte[] b = new byte[1024 * 10];
            int tmp = 0;
            basepath += File.separator;
            if (file.isFile()) {
                zos.putNextEntry(new ZipEntry(basepath));
                is = new FileInputStream(file);
                while ((tmp = is.read(b, 0, b.length)) != -1) {
                    zos.write(b, 0, tmp);
                }
                is.close();
            } else {
                for (File f : file.listFiles()) {
                    PutEntry(zos, f, basepath + f.getName());
                }
            }
            zos.flush();
        }

        /**
         * 解壓縮
         */
        private static void Unzip() {
            String file = "d:/testzip.zip";
            File zipFile = new File(file);
            int i = file.lastIndexOf(".");
            File filePath = new File(file.substring(0, i));
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            File temp = null;
            try {
                ZipFile zip = new ZipFile(zipFile, "UTF-8");
                ZipEntry entry = null;
                byte[] b = new byte[1024 * 10];
                int tmp = 0;
                
                FileOutputStream os = null;
                InputStream is = null;
                for (Enumeration enu = zip.getEntries(); enu.hasMoreElements();) {
                    entry = (ZipEntry) enu.nextElement();
                    is = zip.getInputStream(entry);
                    temp = new File(filePath.getParent() + File.separator + entry.getName());
                    if (!temp.getParentFile().exists()) {
                        temp.getParentFile().mkdirs();
                    }
                    os = new FileOutputStream(temp);
                    while ((tmp = is.read(b, 0, b.length)) != -1) {
                        os.write(b, 0, tmp);
                    }
                    os.flush();
                    os.close();
                    is.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    posted on 2011-08-04 09:29 墻頭草 閱讀(1475) 評論(0)  編輯  收藏

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


    網站導航:
     
    人人游戲網 軟件開發網 貨運專家
    主站蜘蛛池模板: 亚洲区精品久久一区二区三区| 亚洲精品无码精品mV在线观看| 国产成+人+综合+亚洲专| 免费精品无码AV片在线观看| 亚洲AV无码精品色午夜果冻不卡 | 亚洲成AV人片一区二区密柚| 一级女性全黄生活片免费看| 亚洲区小说区图片区| xxxxx做受大片视频免费| 亚洲人成无码网WWW| 国产精品免费久久久久影院| 亚洲а∨天堂久久精品9966| 九九九精品成人免费视频| 亚洲香蕉久久一区二区| 日韩视频免费在线| 人人爽人人爽人人片av免费| 亚洲精品高清国产一线久久| 免费A级毛片无码A∨中文字幕下载| 91亚洲精品视频| 天天拍拍天天爽免费视频| 色吊丝免费观看网站| 国产亚洲情侣一区二区无| 国产成人一区二区三区视频免费| 亚洲黄色免费网站| 国产老女人精品免费视频| 一级人做人a爰免费视频| 亚洲AV区无码字幕中文色| 无码国产精品一区二区免费式直播 | 色妞WWW精品免费视频| 国产亚洲欧美在线观看| 亚洲精品一级无码鲁丝片| 性无码免费一区二区三区在线 | 免费成人福利视频| 亚洲日本天堂在线| 亚洲伊人久久大香线蕉综合图片| 最近最新高清免费中文字幕| 亚洲爆乳成av人在线视菜奈实 | 亚洲youjizz| 久久精品国产亚洲7777| 亚洲免费在线视频观看| 深夜特黄a级毛片免费播放|