文件压缩

public static void zipFileList(String zipFileName, List<String> fileNameList) throws IOException {
        List<File> fileList = new ArrayList<File>();
        for(String fileName : fileNameList){
            File f = new File(fileName);
            fileList.add(f);
        }
        File zipFile = new File(zipFileName);
        if (!zipFile.exists()) {
            if (zipFile.getParentFile() != null
                    && !zipFile.getParentFile().exists()) {
                zipFile.getParentFile().mkdirs();
            }
            zipFile.createNewFile();
        }
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));
        for(File f : fileList){
            out.putNextEntry(new org.apache.tools.zip.ZipEntry(f.getName()));
            FileInputStream in = new FileInputStream(f);
            int b;
            while ((b = in.read()) != -1) {
                out.write(b);
            }
            in.close();
        }
        out.close();
    }

猜你喜欢

转载自even-ing.iteye.com/blog/2287704
今日推荐