10

我想在我的程序中做这样的事情:

File zipFile = .....;
File destDir = ....;    
ImaginaryZipUtility.unzipAllTo(zipFile, destdir);

我不可能是第一个从程序中做到这一点的人。我在哪里可以找到像上面这样的实用方法?我试图查看 apache commons-io,但那里什么也没有。那么,我应该去哪里看呢?

4

4 回答 4

7

我能够挖掘的非常旧的代码

package com.den.frontend;

import java.util.*;
import java.util.zip.*;
import java.io.*;

public class ZIPUtility 
    {
        public static final int BUFFER_SIZE = 2048;

        //This function converts the zip file into uncompressed files which are placed in the 
        //destination directory
        //destination directory should be created first
        public static boolean unzipFiles(String srcDirectory, String srcFile, String destDirectory)
        {
            try
            {
                //first make sure that all the arguments are valid and not null
                if(srcDirectory == null)
                {
                    System.out.println(1);
                    return false;
                }
                if(srcFile == null)
                {
                    System.out.println(2);
                    return false;
                }
                if(destDirectory == null)
                {
                    System.out.println(3);
                    return false;
                }
                if(srcDirectory.equals(""))
                {
                    System.out.println(4);
                    return false;
                }
                if(srcFile.equals(""))
                {   
                    System.out.println(5);
                    return false;
                }
                if(destDirectory.equals(""))
                {
                    System.out.println(6);
                    return false;
                }
                //now make sure that these directories exist
                File sourceDirectory = new File(srcDirectory);
                File sourceFile = new File(srcDirectory + File.separator + srcFile);
                File destinationDirectory = new File(destDirectory);

                // Prevent "Zip Slip" vulnerability https://snyk.io/research/zip-slip-vulnerability
                String canonicalDestinationFile = sourceFile.getCanonicalPath();
                if (!canonicalDestinationFile.startsWith(destinationDirectory.getCanonicalPath() + File.separator)) {
                    throw new Exception("Entry is outside of the target dir: " + e.getName());
                }

                if(!sourceDirectory.exists())
                {
                    System.out.println(7);
                    return false;
                }
                if(!sourceFile.exists())
                {
                    System.out.println(sourceFile);
                    return false;
                }
                if(!destinationDirectory.exists())
                {
                    System.out.println(9);
                    return false;
                }

                //now start with unzip process
                BufferedOutputStream dest = null;

                FileInputStream fis = new FileInputStream(sourceFile);
                ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

                ZipEntry entry = null;

                while((entry = zis.getNextEntry()) != null)
                {
                    String outputFilename = destDirectory + File.separator + entry.getName();

                    System.out.println("Extracting file: " + entry.getName());

                    createDirIfNeeded(destDirectory, entry);

                    int count;

                    byte data[] = new byte[BUFFER_SIZE];

                    //write the file to the disk
                    FileOutputStream fos = new FileOutputStream(outputFilename);
                    dest = new BufferedOutputStream(fos, BUFFER_SIZE);

                    while((count = zis.read(data, 0, BUFFER_SIZE)) != -1)
                    {
                        dest.write(data, 0, count);
                    }

                    //close the output streams
                    dest.flush();
                    dest.close();
                }

                //we are done with all the files
                //close the zip file
                zis.close();

            }
            catch(Exception e)
            {
                e.printStackTrace();
                return false;
            }

            return true;
        }

        private static void createDirIfNeeded(String destDirectory, ZipEntry entry)
        {
            String name = entry.getName();

            if(name.contains("/"))
            {
                System.out.println("directory will need to be created");

                int index = name.lastIndexOf("/");
                String dirSequence = name.substring(0, index);

                File newDirs = new File(destDirectory + File.separator + dirSequence);

                //create the directory
                newDirs.mkdirs();
            }
        }

    }
于 2010-08-31T20:00:42.250 回答
6

我知道我来晚了,但我一直在寻找同样的东西,并通过谷歌找到了这个。我发现最简单的方法是蚂蚁任务“解压缩”。你可以在没有任何复杂的 ant 设置的情况下从你的 java 源代码中使用它(有像 ProjectHelper 这样的东西来构建一个完整的 ant 项目),包括

<dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant-compress</artifactId>
    <version>1.2</version>
</dependency>

将文件解压缩到目录的代码如下所示

org.apache.ant.compress.taskdefs.Unzip u = new Unzip();
u.setSrc(new File("<archive.zip>"));
u.setDest(new File("<targetDir>"));
u.execute();
于 2013-01-31T07:28:02.477 回答
0

好吧,java.util.zip有类可以以非常直接的方式做你想做的事

于 2010-08-31T19:52:46.127 回答
0

似乎可以使用 TrueZip 库做我想做的事: https ://truezip.dev.java.net/manual-6.html#Copying

这不是一个理想的情况,因为库非常大并且范围比我需要的更大(并且还有一些特殊和令人困惑的细节,例如围绕 java.io.File 的子类进行组织,这些子类也称为 File 用于通常还处理 java.io.File 实例的类!)。

至少我不必处于类中的大多数代码行与类的职责无关的情况,或者在项目中维护与模块目的无关的复杂实用程序类.

我认为这是一个典型的例子,说明有经验的开发人员从 Java 迁移到 Ruby 的主要原因。尽管 java 中有大量的库,但其中有太多设计不佳,以至于简单的操作变得与更专业的操作一样难以执行。似乎它们是由技术专家自下而上编写的,他们更渴望公开所有细节和可能性,而不是让日常任务变得简单。apache commons 的人们应该获得荣誉,因为他们创建的库可以让你的类从与类的业务目的无关的代码行中解脱出来,尤其是循环和条件语句。

于 2010-09-01T10:55:23.790 回答