import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Decompression {
public static void main(String[] args) {
String tat = "F:\\WDJDownload\\question.zip";
String tatout = "F:\\WDJDownload\\out\\";
unzipZipFile(tat, tatout);
}
public static final void writeFile(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
public static void unzipZipFile(String zipFileName, String directoryToExtractTo) {
@SuppressWarnings("rawtypes")
Enumeration entriesEnum;
ZipFile zipFile;
try {
zipFile = new ZipFile(zipFileName);
entriesEnum = zipFile.entries();
File directory = new File(directoryToExtractTo);
if (!directory.exists()) {
directory.mkdir();
System.err.println("...Directory Created -" + directoryToExtractTo);
}
while (entriesEnum.hasMoreElements()) {
try {
ZipEntry entry = (ZipEntry)entriesEnum.nextElement();
if (entry.isDirectory()) {
/**
* Currently not unzipping the directory structure.
* All the files will be unzipped in a Directory
*
**/
} else {
System.err.println("Extracting file: " + entry.getName());
/**
* The following logic will just extract the file name
* and discard the directory
*/
int index = 0;
String name = entry.getName();
index = entry.getName().lastIndexOf("/");
if (index > 0 && index != name.length())
name = entry.getName().substring(index + 1);
System.out.println(name);
writeFile(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(directoryToExtractTo + name)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
zipFile.close();
} catch (IOException ioe) {
System.err.println("Some Exception Occurred:");
ioe.printStackTrace();
return;
}
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class Decompression {
public static void main(String[] args) {
String tat = "F:\\WDJDownload\\question.zip";
String tatout = "F:\\WDJDownload\\out\\";
unzipZipFile(tat, tatout);
}
public static final void writeFile(InputStream in, OutputStream out)
throws IOException {
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);
in.close();
out.close();
}
public static void unzipZipFile(String zipFileName, String directoryToExtractTo) {
@SuppressWarnings("rawtypes")
Enumeration entriesEnum;
ZipFile zipFile;
try {
zipFile = new ZipFile(zipFileName);
entriesEnum = zipFile.entries();
File directory = new File(directoryToExtractTo);
if (!directory.exists()) {
directory.mkdir();
System.err.println("...Directory Created -" + directoryToExtractTo);
}
while (entriesEnum.hasMoreElements()) {
try {
ZipEntry entry = (ZipEntry)entriesEnum.nextElement();
if (entry.isDirectory()) {
/**
* Currently not unzipping the directory structure.
* All the files will be unzipped in a Directory
*
**/
} else {
System.err.println("Extracting file: " + entry.getName());
/**
* The following logic will just extract the file name
* and discard the directory
*/
int index = 0;
String name = entry.getName();
index = entry.getName().lastIndexOf("/");
if (index > 0 && index != name.length())
name = entry.getName().substring(index + 1);
System.out.println(name);
writeFile(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(directoryToExtractTo + name)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
zipFile.close();
} catch (IOException ioe) {
System.err.println("Some Exception Occurred:");
ioe.printStackTrace();
return;
}
}
}