Possible Duplicate:
How do I list the files inside a JAR file?
That's mainly the question, how to get the files inside a zip file as an array of files
Possible Duplicate:
How do I list the files inside a JAR file?
That's mainly the question, how to get the files inside a zip file as an array of files
Apart from extracting the files from the zip, I don't know an URI scheme that references a file directly in the zip file. So you will end with a list of filenames as String
not File
instances.
So you may use classes like ZipFile
, ZipEntry
, input streams and son on to extract files from the Zip, then you can have a File instance referencing it in an array.
The simple answer is that you can't. A File
is essentially an abstracted file system path for an object in the host operating system's file system. But a file in a ZIP file is not an object in the filesystem, and therefore doesn't have a file system path.
Think about it. Can you formulate a <pathname>
for a ZIP file entry that would allow you to (say) run cat <pathname>
on Linux? Or something similar on Windows?
So what can you do?
You could extract the ZIP file to the file system (using an external command would be simplest) and then refer to the files containing the extracted entries. You would need to mess around a bit to turn the ZIP's directory structure into the corresponding pathnames.
You could use ZipEntry
objects instead of File objects ... though I think that they will only be valid / usable while the Zip file remains open.
You could find ... or write a Java 7-style FileSystem
implementation that allowed you to treat a ZIP file as a file system. But those API's don't use File
objects, etcetera.
If your operating system supported it, you could "mount" the ZIP file as a file system, and then File
would work. For example: http://en.wikipedia.org/wiki/Filesystem_in_Userspace plus http://code.google.com/p/fuse-zip/
You may want to check out java.util.zip
package that has utilities to unzip files.
To get an array of files inside a zip, they must first be extracted into a temporary location:
import java.util.zip.*;
import java.io.File;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.io.IOException;
public class ZipFileExtractor {
public static void main(String[] args) {
File[] zipFiles = ZipFileExtractor.getFileArray("archive.zip");
System.out.println(zipFiles.length);
}
public static File[] getFileArray(String archiveFileName) {
ArrayList<File> zipArrayList = new ArrayList<File>();
try {
ZipFile zFile = new ZipFile(archiveFileName);
for (Enumeration<? extends ZipEntry> e = zFile.entries(); e.hasMoreElements(); ) {
ZipEntry zEntry = e.nextElement();
InputStream in = zFile.getInputStream(zEntry);
File temp = File.createTempFile(zFile.getName(), null);
temp.deleteOnExit();
FileOutputStream out = new FileOutputStream(temp);
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
}
zipArrayList.add(temp);
}
} catch (IOException e) {
}
return zipArrayList.toArray(new File[zipArrayList.size()]);
}
}
There is an API class called ZipInputStream in java.util.zip
package.
It has a bunch of methods, among them getNextEntry()
that pretty much does it:
ArrayList<ZipEntry> zipArrayList = new ArrayList<ZipEntry>();
ZipInputStream zis = ZipInputStream(new FileInputStream("archive.zip"));
while ((ZipEntry ze = zis.getNextEntry()) != null) {
zipArrayList.add(ze.getName());
}
I haven't tried compiling the above code, so it might have a few issues but I hope the idea is clear.
Also check out the Apache Commons Compress api there are a few example on how to get started http://commons.apache.org/compress/examples.html