Java: Zip file with non-static filename

前端 未结 1 1744
南旧
南旧 2021-01-26 14:44

I found this ZipUtils class on this post: how to zip a folder itself using java

I modified it so I could pass a zip file name. However, the only way it works is with a h

相关标签:
1条回答
  • 2021-01-26 15:31

    There are probably hundreds of ways you could fix this depending on your needs, but from my perspective, what you want to be able to do is say "Zip this folder to this zip file" in as few lines of code as possible...

    To this end, I could alter the code to allow you to do something like...

    ZipUtils appZip = new ZipUtils();
    appZip.zipIt(new File(source), new File(output));
    

    The use of File leaves no ambiguity as to the meaning of the parameters. This mechanism also means that you can call zipIt again and again and again based on your needs, without having to create a new instance of ZipUtils

    This will require some modification to the base code, as it assumes String values for the file paths, which frankly is just maddening, as all the information you really want can be more easily obtained from the File object - IMHO. This also means that you don't need to maintain a reference to the source path at all, beyond the scope of the zipIt method

    public static class ZipUtils {
    
        private final List<File> fileList;
    
        private List<String> paths;
    
        public ZipUtils() {
            fileList = new ArrayList<>();
            paths = new ArrayList<>(25);
        }
    
        public void zipIt(File sourceFile, File zipFile) {
            if (sourceFile.isDirectory()) {
                byte[] buffer = new byte[1024];
                FileOutputStream fos = null;
                ZipOutputStream zos = null;
    
                try {
    
                    String sourcePath = sourceFile.getPath();
                    generateFileList(sourceFile);
    
                    fos = new FileOutputStream(zipFile);
                    zos = new ZipOutputStream(fos);
    
                    System.out.println("Output to Zip : " + zipFile);
                    FileInputStream in = null;
    
                    for (File file : this.fileList) {
                        String path = file.getParent().trim();
                        path = path.substring(sourcePath.length());
    
                        if (path.startsWith(File.separator)) {
                            path = path.substring(1);
                        }
    
                        if (path.length() > 0) {
                            if (!paths.contains(path)) {
                                paths.add(path);
                                ZipEntry ze = new ZipEntry(path + "/");
                                zos.putNextEntry(ze);
                                zos.closeEntry();
                            }
                            path += "/";
                        }
    
                        String entryName = path + file.getName();
                        System.out.println("File Added : " + entryName);
                        ZipEntry ze = new ZipEntry(entryName);
    
                        zos.putNextEntry(ze);
                        try {
                            in = new FileInputStream(file);
                            int len;
                            while ((len = in.read(buffer)) > 0) {
                                zos.write(buffer, 0, len);
                            }
                        } finally {
                            in.close();
                        }
                    }
    
                    zos.closeEntry();
                    System.out.println("Folder successfully compressed");
    
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        zos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        protected void generateFileList(File node) {
    
            // add file only
            if (node.isFile()) {
                fileList.add(node);
    
            }
    
            if (node.isDirectory()) {
                File[] subNote = node.listFiles();
                for (File filename : subNote) {
                    generateFileList(filename);
                }
            }
        }
    }
    

    ps- You public static void main, isn't a valid "main entry point", it should be public static void main(String[] args) ;)

    So, based on your code snippet, you could simply do something like...

    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
    Date date = new Date();
    
    String zipName = name+ "_" + dateFormat.format(date) + ".zip";
    zippedFile = new File(archive, zipName);
    if (zippedFile exists()) {
        zippedFile.delete();
    }
    
    ZipUtils zu = new ZipUtils();
    zu.zipIt(dirToZip, zippedFile);
    
    0 讨论(0)
提交回复
热议问题