U. George said:
I'd like to open a zip file of sound bites. This zip file is within a
jar file.
The only way (that I see) to open a zip/jar file requires a File, or
String. Apparently you cant open a zipfile by simply doing "new
ZipFile( ZipFile.getInputStream( ZipEntry ))"
Any Hope?
Short answer, no you can't "open" a ZipFile recursively, i.e. a zipped
file within a zipped file (jar). However, you can read the contents of
a zipped file entry in a jar file using ZipInputStream. Sample Below.
Create the following files;
File: tZipInJar.java
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class tZipInJar
{
public static void main(String[] args) throws Exception
{
ZipFile jarFile = new ZipFile("tZipInJar.jar");
ZipEntry jarEntry = jarFile.getEntry("tZipInJar.zip");
InputStream jarIs = jarFile.getInputStream(jarEntry);
ZipInputStream zipIs = new ZipInputStream(jarIs);
ZipEntry zipEntry = zipIs.getNextEntry();
long size = zipEntry.getSize();
BufferedReader br = new BufferedReader(
new InputStreamReader(jarFile.getInputStream(zipEntry)));
while(0 < size)
{
String line = null;
try {
line = br.readLine();
size -= line.length();
System.out.println(line);
} catch (Exception ex) {
size = 0;
}
}
br.close();
zipIs.close();
jarFile.close();
}
}
File: tZipInJar.zip
command line: zip tZipInJar.zip tZipInJar.java
Compile tZipInJar.java, build a jar file containing the class and the
zip file created from the source and run the jar application. This
example is simplistic, and cludgy but it works.
Joseph