Java Reference
In-Depth Information
Solution
Using the Java.util.zip package, you can open a .zip file and iterate through
its entries. While traversing the entries, directories can be created for directory entries.
Similarly, when a file entry is encountered, write the decompressed file to the file . un-
zipped . The following lines of code demonstrate how to perform the decompress and
file iteration technique, as described.
ZipFile file = null;
try {
file = new ZipFile("file.zip");
FileSystem fileSystem = FileSystems.getDefault();
Enumeration<? extends ZipEntry> entries
= file.entries();
String uncompressedDirectory = "uncompressed/";
Files.createDirectory(fileSystem.getPath(uncompressedDirectory));
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.isDirectory()) {
System.out.println("Creating Directory:"
+ uncompressedDirectory + entry.getName());
Files.createDirectories(fileSystem.getPath(uncompressedDirectory
+
entry.getName()));
} else {
InputStream is = file.getInputStream(entry);
System.out.println("File :"
+ entry.getName());
BufferedInputStream bis = new
BufferedInputStream(is);
String uncompressedFileName
= uncompressedDirectory + entry.getName();
Path uncompressedFilePath
= fileSystem.getPath(uncompressedFileName);
Files.createFile(uncompressedFilePath);
try (FileOutputStream fileOutput = new
FileOutputStream(uncompressedFileName)) {
Search WWH ::




Custom Search