The recording file is compressed to zip, and uncompressed zip

public File compress(String resource, File resourceFile, FileFilter filter, JSONArray subFileMd5List) throws Exception {
        java.util.zip.ZipOutputStream zos = null;
        try {
            String prefix = resource.replaceAll("\\\\", "_");
            prefix = prefix.replaceAll("/", "_");
            while (prefix.length() < 3)
                prefix += "_";
            File file = File.createTempFile(prefix, ".zip");
            logger.info("压缩{}->{}", resource, file);
            FileOutputStream out = new FileOutputStream(file);
            zos = new ZipOutputStream(out);
            compress(resourceFile, zos, resourceFile.getName(), true, filter, subFileMd5List);
            return file;
        } finally {
            if (zos != null) {
                try {
                    zos.close();
                } catch (IOException e) {
                }
            }
        }
    }

    private void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure, FileFilter filter, JSONArray subFileMd5List) throws Exception {         byte[] buf = new byte[4096];         if (filter != null && !filter.accept(sourceFile) ) {             return;         }         if (sourceFile.isFile()) {             // Add a zip entity to the zip output stream. The name of the file in the constructor is the name of the zip entity.             ZipEntry entry = new ZipEntry(name);             entry.setTime( sourceFile.lastModified());             zos.putNextEntry(entry);             // copy the file to the zip output stream             int len;             FileInputStream in = new FileInputStream(sourceFile);












            while ((len = in.read(buf)) != -1) {                 zos.write(buf, 0, len);             }             // Complete the entry             zos.closeEntry();             in.close();             JSONObject obj = new JSONObject();             obj.put("url", name);             obj.put("md5", FileUtil.getFileMD5String(sourceFile));             subFileMd5List.add(obj);         } else {             File[] listFiles = sourceFile.listFiles ();             java.util.Arrays.sort(listFiles);             if (listFiles == null || listFiles.length == 0) {                 // When the original file structure needs to be retained, the empty folder needs to be processed                 if (KeepDirStructure ) {















                    // Handling of empty folders
                    ZipEntry entry = new ZipEntry(name + "/");
                    entry.setTime(sourceFile.lastModified());
                    zos.putNextEntry(entry);
                    // No files, no file copy
                    zos. closeEntry();
                }
            } else {                 for (File file: listFiles) {// Determine whether you need to keep the original file structure                     if (KeepDirStructure) {                         // Note: The name of the parent folder needs to be added before file.getName() A slash,                         // Otherwise, the original file structure cannot be retained in the final compressed package, that is: all files have gone to the root directory of the                         compressed package compress(file, zos, name + "/" + file.getName(), KeepDirStructure, filter, subFileMd5List);





                    } else {
                        compress(file, zos, file.getName(), KeepDirStructure, filter, subFileMd5List);
                    }
                }
            }
        }
    }

    public void decompress(File srcFile, String destDirPath, FileFilter filter) throws Exception {
        try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(srcFile)) {
            Enumeration<?> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = (ZipEntry) entries.nextElement();
                // 如果是文件夹,就创建个文件夹
                if (entry.isDirectory()) {
                    String dirPath = destDirPath + "/" + entry.getName();
                    File dir = new File(dirPath);
                    if (filter == null || filter.accept(dir)) {
                        dir.mkdirs();
                        dir.setLastModified(entry.getTime());
                    }
                } else {                     // If it is a file, first create a file, and then use the io stream to copy the content                     File targetFile = new File(destDirPath + "/" + entry.getName ());                     if (filter == null || filter.accept(targetFile)) {                         // Ensure that the parent folder of this file must exist                         if (!targetFile.getParentFile().exists()) {                             targetFile.getParentFile( ).mkdirs();                         }                         targetFile.createNewFile();                         // Write the contents of the compressed file to this file









                        try (java.io.InputStream is = zipFile.getInputStream(entry); FileOutputStream fos = new FileOutputStream(targetFile);) {
                            int len;
                            byte[] buf = new byte[BUFFER_SIZE];
                            while ((len = is.read(buf)) != -1) {
                                fos.write(buf, 0, len);
                            }
                        }
                        targetFile.setLastModified(entry.getTime());
                    }

                }
            }
        }
    }

Guess you like

Origin blog.csdn.net/jia_dojo/article/details/109282895
Recommended