import java.util.*;
import java.io.*;
import org.apache.commons.compress.archivers.zip.*;
import org.apache.commons.compress.utils.IOUtils;
public void unzip(String ziplocation, String zipname, String unziplocation){
//method variables
private ZipFile zipFile;
private Enumeration<ZipArchiveEntry> entries;
private ZipArchiveEntry entry;
private File entryDestination;
private InputStream in;
private OutputStream out;
zipFile = new ZipFile(ziplocation+File.separator+zipname);
//get zip file content references
entries = zipFile.getEntries();
if(entries != null){
//for each reference, unzip to unziplocation, creating directories if needed
while (entries.hasMoreElements()) {
entry = entries.nextElement();
entryDestination = new File(unziplocation, entry.getName());
entryDestination.getParentFile().mkdirs();
if (entry.isDirectory()){
entryDestination.mkdirs();
}
else {
in = zipFile.getInputStream(entry);
out = new FileOutputStream(entryDestination);
IOUtils.copy(in, out);
//close streams ignoring exceptions
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
}
}
30/08/2014
[Java] unzip
Now that we know how to zip in Java, let's see how to unzip using the same Apache Commons Compress library:
Tag:
Apache,
Java,
Source code
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
With great power comes great responsibility