Hi Folks,
I'm trying to unzip a files that contain other zip files. My attempt is based on the following Java2s code: http://www.java2s.com/Code/Java/File-Input-Output/LoadresourcefromJarfile.htm.
My only difficulty is that when an entry is itself a Jar file, I'm not able to unzip it without writing it to a temp file first.
Here is what I'm doing:
public void load(String jarFileName) throws Exception{
ZipFile zf = new ZipFile(jarFileName);
load(zf.entries(),new FileInputStream(jarFileName),zf.size());
}
public void load(Enumeration<? extends ZipEntry> enumeration, InputStream inputStream, int total) throws Exception {
if(enumeration!=null){
while (enumeration.hasMoreElements()) {
ZipEntry ze = (ZipEntry) enumeration.nextElement();
htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
}
}
BufferedInputStream bis = new BufferedInputStream(inputStream);
ZipInputStream zis = new ZipInputStream(bis);
ZipEntry ze = null;
int retrieved=0;
while ((ze = zis.getNextEntry()) != null) {
if (ze.isDirectory()) {
continue;
}
int size = (int) ze.getSize();
if (size == -1 && htSizes.get(ze.getName())!=null) {
size = ((Integer) htSizes.get(ze.getName())).intValue();
}
if(size==-1)
size=total-retrieved;
retrieved+=size;
byte[] b = new byte[(int) size];
int rb = 0;
int chunk = 0;
while (((int) size - rb) > 0) {
chunk = zis.read(b, rb, (int) size - rb);
if (chunk == -1) {
break;
}
rb += chunk;
}
if(ze.getName().endsWith(".jar")){
File f=File.createTempFile("temp", System.nanoTime()+"");
f.deleteOnExit();
FileOutputStream fos= new FileOutputStream(f);
fos.write(b);
fos.close();
load(f.getAbsolutePath());
}
else htJarContents.put(ze.getName(), b);
}
}
What I really want to do, however is this:
public void load(String jarFileName) throws Exception{
ZipFile zf = new ZipFile(jarFileName);
load(zf.entries(),new FileInputStream(jarFileName),zf.size());
}
public void load(Enumeration<? extends ZipEntry> enumeration, InputStream inputStream, int total) throws Exception {
if(enumeration!=null){
while (enumeration.hasMoreElements()) {
ZipEntry ze = (ZipEntry) enumeration.nextElement();
htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
}
}
BufferedInputStream bis = new BufferedInputStream(inputStream);
ZipInputStream zis = new ZipInputStream(bis);
ZipEntry ze = null;
int retrieved=0;
while ((ze = zis.getNextEntry()) != null) {
if (ze.isDirectory()) {
continue;
}
int size = (int) ze.getSize();
if (size == -1 && htSizes.get(ze.getName())!=null) {
size = ((Integer) htSizes.get(ze.getName())).intValue();
}
if(size==-1)
size=total-retrieved;
retrieved+=size;
byte[] b = new byte[(int) size];
int rb = 0;
int chunk = 0;
while (((int) size - rb) > 0) {
chunk = zis.read(b, rb, (int) size - rb);
if (chunk == -1) {
break;
}
rb += chunk;
}
if(ze.getName().endsWith(".jar")){
load(null,new ByteArrayOutputstream(b));
}
else htJarContents.put(ze.getName(), b);
}
}
What I'm seeing wit the second version is that the size information of the entries contained in the inner-jars are available only if I write this inner jar to a the disk first.
is there a way to avoid this?
Thanks so much, Klaus.