Description
Unzips the given zip file into the given folder retaining the folder structure of the zip.
License
Open Source License
Parameter
Parameter | Description |
---|
zip | the zip to unzip |
folder | the folder to place the contents of the zip |
Exception
Parameter | Description |
---|
ZipException | thrown if the zip file is invalid |
FileNotFoundException | thrown if the given file is not found |
IOException | thrown if an IO error occurs |
IllegalStateException | thrown if the zip file is closed prematurely |
SecurityException | thrown if a security check fails |
Declaration
public static final void unzip(String zip, String folder)
throws ZipException, FileNotFoundException, IOException, IllegalStateException, SecurityException
Method Source Code
//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
public class Main {
/** The local buffer size */
private static final int BUFFER_SIZE = 1024;
/**/* w ww. java 2s .co m*/
* Unzips the given zip file into the given folder retaining the folder structure of the zip.
* <p>
* This method does not use the name of the zip file as the top level folder.
* @param zip the zip to unzip
* @param folder the folder to place the contents of the zip
* @throws ZipException thrown if the zip file is invalid
* @throws FileNotFoundException thrown if the given file is not found
* @throws IOException thrown if an IO error occurs
* @throws IllegalStateException thrown if the zip file is closed prematurely
* @throws SecurityException thrown if a security check fails
*/
public static final void unzip(String zip, String folder)
throws ZipException, FileNotFoundException, IOException, IllegalStateException, SecurityException {
// verify input
if (zip == null || zip.length() == 0 || folder == null || folder.length() == 0) {
// in any of these cases just silently return
return;
}
// get a file reference to the zip
try (ZipFile zipFile = new ZipFile(zip)) {
// get all the entries
Enumeration<? extends ZipEntry> entries = zipFile.entries();
// loop through all the entries
while (entries.hasMoreElements()) {
// get the entry
ZipEntry entry = entries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(folder, currentEntry);
File destinationParent = destFile.getParentFile();
// create the path if necessary
destinationParent.mkdirs();
// check for file type
if (!entry.isDirectory()) {
int bytesRead;
// establish buffer for writing file
byte data[] = new byte[BUFFER_SIZE];
// read the entry and write it to the location
try (BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER_SIZE);) {
// read and write until last byte is encountered
while ((bytesRead = is.read(data, 0, BUFFER_SIZE)) != -1) {
dest.write(data, 0, bytesRead);
}
}
}
}
}
}
}
Related
- unzip(String sourceZip, String targetDirectory)
- unzip(String src, String dest, PrintStream stream)
- unzip(String sZip)
- unzip(String toUnpackName)
- unzip(String zip, String dest)
- unzip(String zip, String path)
- unzip(String zip, String unzipDir, int bufferSize)
- Unzip(String zipFile, String outputDirectory)
- unzip(String zipFile, String targetFolder)