JAVA打包文件成压缩文件


    public static String createZip(List<String> paths, String fileDir, String version) throws IOException {
        String zipFile = getTempFile(fileDir, ContestConstants.SUFFIX_ZIP, version);
        FileOutputStream out = new FileOutputStream(zipFile);
        ZipOutputStream zos = new ZipOutputStream(out);
        for (String path : paths) {
            File srcFile = new File(path);
            try {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
                int len;
                byte[] buf = new byte[10 * 1024];
                while ((len = bis.read(buf, 0, buf.length)) != -1) {
                    zos.write(buf, 0, len);
                }
                bis.close();
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                zos.closeEntry();
            }
        }
        return zipFile;
    }

猜你喜欢

转载自blog.csdn.net/m0_37574375/article/details/110947237