There is a zip file which contains a folder inside it. The folder itself contains a few files. I would need to know how to extract the folder (with its contents) from inside a zip file.
I have found a few unzipping code samples which show how to handle a folder inside a zip file. An example is shown below:
Code:
public static void extract(String workingDirectory, byte[] zipFile)
throws Exception {
ByteArrayInputStream byteStream = new ByteArrayInputStream(zipFile);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipInputStream zipStream = new ZipInputStream(byteStream);
ZipEntry zipEntry = null;
String nameZipEntry = null;
byte[] contentZipEntry = null;
boolean isDirectory = false;
int indexFileSeparator = -1;
String directory = null;
String fileName = null;
while ( (zipEntry = zipStream.getNextEntry()) != null )
{
nameZipEntry = workingDirectory + File.separator + zipEntry.getName();
isDirectory = zipEntry.isDirectory();
if (isDirectory) {
File file = new File(nameZipEntry);
file.mkdirs();
}
else
{
// read zipEntry
byte[] buf = new byte[1024];
int c = 0;
while ( (c = zipStream.read(buf)) != -1)
{
out.write(buf, 0, c);
}
indexFileSeparator = nameZipEntry.lastIndexOf(File.separator);
directory = nameZipEntry.substring(0, indexFileSeparator);
fileName = nameZipEntry.substring(indexFileSeparator+1,nameZipEntry.length());
FileSystemTools.createFile(directory, fileName, out);
out.reset();
zipStream.closeEntry();
}
}
zipStream.close();
byteStream.close();
}
The code sample which deals with the part where the zipEntry is a directory creates a directory with the same path and name. (highlighted in bold)
Another similar variation is:
Code:
File file = new File(dirDestiny.getAbsolutePath() + File.separator + zipEntry.getName() );
if(zipEntry.isDirectory())
file.mkdirs();
When the code creates a directory for the folder, does it unzip the contents inside the folder as well?
If not, how do I extract the files inside the folder?