在Java中,解压整个归档文件到目录的实用工具

11

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

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

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


我在Apache commons-compress中添加了此功能请求: https://issues.apache.org/jira/browse/COMPRESS-118 - eirikma
7
现在已经是2011年,甚至没有一个(通用的)第三方库可以通过单个调用在Java中提取ZIP文件?WTF。 - Kutzi
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();
            }
        }

    }

2
谢谢,你们俩。但是那不是我想要的。我知道使用std api可以遍历zip entries、创建所需的目录等等。但我不想在我的程序中加入那些代码。太复杂了,而且不好看。所有想要解压缩文件的程序都不需要包含所有这些内容。我希望有一个单行命令来解压缩一个文档,就像在命令行上一样。很难相信ZipFile类没有一个unzipTo(directory)方法,而需要50行代码才能完成相同的操作。这API设计是什么意思? - eirikma
1
也许我应该尝试这个: Runtime.getRuntime().exec(new String[]{ "unzip", zipfile.getAbsolutePath(), "-d", destdir.getAbsolutePath(), }); - eirikma
使用JavaClass的优点是可移植性(您不必担心本地二进制文件)。 - helios
3
为什么不将上述代码作为一个类添加到你的项目中呢?这样你就能得到一行代码的解决方案了!例如:ZipUtility.unzipFiles("/srcDir", "myZipFile.zip", "/destination"); - Andrew Dyster
@eirikma:我认为这些库的重点是提供通用工具,而你的应用程序必须根据自己的情况进行适应。Apache专门制作实用类(提供打包类-方法来实现库的常见场景使用)。我不知道有没有zip实用程序(提供你想要的内容)。但总的来说,我会编写一个,将其保存在某个地方作为库并使用它。如果我后来发现有一个被接受的实用程序,我可以更改应用程序代码甚至我的特定实用类来使用它。 - helios

6

我知道我来晚了,但我正在寻找同样的事情,并通过谷歌发现了这个。我发现最简单的方法是使用ant任务“unzip”。你可以在不需要复杂的ant设置(例如ProjectHelper构建完整的ant项目)的情况下从你的Java源代码中包含它。

<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();

1

看起来使用TrueZip库可以实现我想要的功能: https://truezip.dev.java.net/manual-6.html#Copying

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

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

我认为这是有经验的开发人员从Java迁移到Ruby的主要原因的典型例子。尽管Java中有大量的库,但其中太多都设计得很差,以至于简单操作变得像更专业的操作一样困难。似乎它们是由技术专家从底层开始编写的,他们更渴望展示所有细节和可能性,而不是使日常任务变得简单。Apache Commons的人们因创建解除您的类中与业务目的无关的代码行(特别是循环和条件语句)的库而受到赞誉。


嗯,结果并不完全符合我的预期。它递归进入了zip文件中的jar文件并提取它们,尽管我使用的代码手册上说不会这样做。这不是我想要的。 - eirikma
尝试使用TrueZIP 7。该类现在称为TFile,其API干净且高效。如果您喜欢Ruby的方法,您也可能会喜欢这个。 - Christian Schlichtherle

0

嗯,java.util.zip有一些类可以以非常简单明了的方式完成你想要的功能。


我知道,但那不是我的意思。请看下面jsshah的回答中的评论。 - eirikma
1
我在那里找不到任何一行代码。我错过了什么吗,@helios? - Chris Beach
不,没有一行代码,只有实现所需功能的工具。读取zip文件(其条目),为每个条目创建目录,打开文件,将内容转储到其中,然后关闭。就像jsshah建议的那样,但可能更紧凑(并使用commons-fileupload的“Streams.copy”)。 - helios

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接