java 文件/文件夹复制,添加压缩zip

复制文件夹,并压缩成zip

需求:创建A文件夹,把B文件夹复制到A文件夹。然后把A文件夹压缩成zip包

 public static void main(String[] args) throws Exception {
   try {
            String A = "D:\\dev\\program";
            String B = "D:\\program";
            // 创建临时文件夹
            File tempDir = new File(A);
            tempDir.mkdir();


            // 把B文件夹复制到A文件夹
            File sourceDir = new File(B);
            File destinationDir = new File(tempDir, "");
            copyDirectory(sourceDir, destinationDir);

            //把A文件夹压缩成zip包
            String AZIP = A + ".zip";
            zipFolder(A, AZIP);

            // 删除临时文件夹
            FileUtils.deleteDirectory(tempDir);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


/**
     * 复制文件夹
     *
     * @param source      复制后文件夹路径
     * @param destination 原文件夹路径
     * @throws IOException
     */
    private static void copyDirectory(File source, File destination) throws IOException {
        File[] files = source.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                copyDirectory(file, new File(destination, file.getName()));
            } else {
                if (!destination.exists()) {
                    destination.mkdirs();
                }
                File copy = new File(destination, file.getName());
                copy.createNewFile();
                FileInputStream in = new FileInputStream(file);
                FileOutputStream out = new FileOutputStream(copy);
                byte[] buffer = new byte[1024];
                int length;
                while ((length = in.read(buffer)) > 0) {
                    out.write(buffer, 0, length);
                }
                in.close();
                out.close();
            }
        }
    }

    /**
     * 压缩文件
     *
     * @param path    需要压缩的文件路径
     * @param zipPath 压缩的文件后路径
     * @throws Exception
     */
    public static void zipFolder(String path, String zipPath) throws Exception {
        FileOutputStream fos = null;
        ZipOutputStream zos = null;
        try {
            fos = new FileOutputStream(zipPath);
            zos = new ZipOutputStream(fos);
            addFolderToZip("", new File(path), zos);
        } finally {
            if (zos != null) {
                zos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }

    /**
     * 秭归添加压缩路径
     */
    private static void addFolderToZip(String parentPath, File folder, ZipOutputStream zos) throws Exception {
        for (File file : folder.listFiles()) {
            if (file.isDirectory()) {
                addFolderToZip(parentPath + folder.getName() + "/", file, zos);
            } else {
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(file);
                    ZipEntry zipEntry = new ZipEntry(parentPath + folder.getName() + "/" + file.getName());
                    zos.putNextEntry(zipEntry);
                    byte[] bytes = new byte[1024];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zos.write(bytes, 0, length);
                    }
                } finally {
                    if (fis != null) {
                        fis.close();
                    }
                }
            }
        }
    }

java 文件/文件夹复制,添加压缩zip_第1张图片

java 文件/文件夹复制,添加压缩zip_第2张图片

没了。谢谢

你可能感兴趣的:(java,python,开发语言)