Say I have a .zip file called Bundles.zip and directly inside Bundles.zip, there are a few files and a few folders. This is what the .zip looks like:
Now, I want to extract EVERYTHING from the Bundles folder. My program already knows the name of the folder that it needs to extract the files from, in this case, Bundles.
The Bundles folder inside the zip can have files, subfolders, files in the subfolders, basically anything, like this:
And I just need to extract everything from the Bundles folder to an output directory.
How can I accomplish this in Java? I have found answers which extract all the files and folders inside the zip, but I need to extract only a specific folder inside the zip, not everything.
Working code so far:
ZipFile zipFile = new ZipFile(mapsDirectory + "mapUpload.tmp");
Enumeration zipEntries = zipFile.entries();
String fname;
String folderToExtract = "";
String originalFileNameNoExtension = originalFileName.replace(".zip", "");
while (zipEntries.hasMoreElements()) {
ZipEntry ze = ((ZipEntry)zipEntries.nextElement());
fname = ze.getName();
if (ze.isDirectory()) //if it is a folder
{
if(originalFileNameNoExtension.contains(fname)) //if this is the folder that I am searching for
{
folderToExtract = fname; //the name of the folder to extract all the files from is now fname
break;
}
}
}
if(folderToExtract == "")
{
printError(out, "Badly packaged Unturned map error: " + e.getMessage());
return;
}
//now, how do i extract everything from the folder named folderToExtract?
For the code so far, the originalFileName is something like "The Island.zip". Inside the zip there is a folder called The Island. I need to find the folder inside the zip file that matches the zip file's name, and extract everything inside that.