I'd like to do something like this in my program:
File zipFile = .....;
File destDir = ....;
ImaginaryZipUtility.unzipAllTo(zipFile, destdir);
I cannot possibly be the first to do this from a program. Where do I find a utility method like above? I tried to look at apache commons-io, but nothing there. So, where should I l开发者_如何转开发ook?
Very old code that I was able to dig up
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();
}
}
}
I know I'm, pretty late to the show, but I was looking for the same thing and found this via google. The easiest way I found is the ant task "unzip". You can use it without any complicated ant setup (there is stuff like ProjectHelper to build a complete ant project) from your java source by including
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant-compress</artifactId>
<version>1.2</version>
</dependency>
The code to unzip a file to a directory looks like this
org.apache.ant.compress.taskdefs.Unzip u = new Unzip();
u.setSrc(new File("<archive.zip>"));
u.setDest(new File("<targetDir>"));
u.execute();
Well, java.util.zip has classes to do what you want in a very straighforward way
It seems like it's possible to do what I want using the TrueZip library: https://truezip.dev.java.net/manual-6.html#Copying
This is not an ideal situation, since the library is quite large and with a larger scope than I need (and also with some peculiar and confusing details such as being organized around subclasses of java.io.File which are also called File for use in classes that typically also handles java.io.File instances!).
At least I don't have to be in a situation where a majority of the code lines in the class are unrelated to the responsibility of the class, or to maintain a complex utility class in the project that is unrelated to the purpose of the module.
I think this is a typical example on the main reason why experienced developers are migrating from Java to Ruby. Despite an abundance of libraries in java, too many of them are poorly designed so that simple operations become just as difficult to perform as the more specialized ones. Seems like they are written from the bottom up by technology experts more eager to expose all details and possibilities than to make everyday tasks simple. The apache commons people deserves honor for creating libraries that relieve your class from code lines, especially loops and conditionals, that are unrelated to the business purpose of the class.
精彩评论