我想在我的程序中做类似这样的事情:
File zipFile = .....;
File destDir = ....;
ImaginaryZipUtility.unzipAllTo(zipFile, destdir);
我肯定不可能是第一个从程序中做这件事的人。我在哪里可以找到像上面那样的实用方法?我尝试查看了apache commons-io,但没有找到。那么,我应该去哪里找呢?
我想在我的程序中做类似这样的事情:
File zipFile = .....;
File destDir = ....;
ImaginaryZipUtility.unzipAllTo(zipFile, destdir);
我肯定不可能是第一个从程序中做这件事的人。我在哪里可以找到像上面那样的实用方法?我尝试查看了apache commons-io,但没有找到。那么,我应该去哪里找呢?
我找到了一些非常古老的代码
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();
}
}
}
我知道我来晚了,但我正在寻找同样的事情,并通过谷歌发现了这个。我发现最简单的方法是使用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();
看起来使用TrueZip库可以实现我想要的功能: https://truezip.dev.java.net/manual-6.html#Copying
这并不是一个理想的情况,因为该库相当大且范围比我需要的要大(还有一些奇特和令人困惑的细节,例如组织在java.io.File的子类周围,这些子类也被称为File,用于通常处理java.io.File实例的类!)。
至少我不必处于大多数代码行与类的责任无关或在项目中维护与模块目的无关的复杂实用程序类的情况下。
我认为这是有经验的开发人员从Java迁移到Ruby的主要原因的典型例子。尽管Java中有大量的库,但其中太多都设计得很差,以至于简单操作变得像更专业的操作一样困难。似乎它们是由技术专家从底层开始编写的,他们更渴望展示所有细节和可能性,而不是使日常任务变得简单。Apache Commons的人们因创建解除您的类中与业务目的无关的代码行(特别是循环和条件语句)的库而受到赞誉。
嗯,java.util.zip有一些类可以以非常简单明了的方式完成你想要的功能。