d.moeller
October 4th, 1999, 03:33 AM
Hi,
I'm using these lines to open a zip file, write an entry and fill the entry with data:
ZipOutputStream blas = new ZipOutputStream(new FileOutputStream("testing.zip"));
ZipEntry ze = new ZipEntry("knut.txt");
blas.putNextEntry(ze);
byte blab[];
String str = new String("HALLO");
blab = str.getBytes();
blas.write(blab, 0, blab.length);
blas.finish();
Now, this creates a new zip file everytime I do it. But I'd rather like to add entries to that zip file, I've tried to open it this way:
ZipOutputStream blas = new ZipOutputStream(new FileOutputStream("testing.zip", true));
But this gives a corrupt zip file - because it appends the basic file, but not the zip file in its structure...
Does anyone know how to do it?
October 18th, 1999, 10:39 AM
I've recently had solve this problem myself. You must use ZipInputStream to open the existing zip file and ZipOuputStream to write it back out then write the additional entries to 'append' all a s uncompressed data. Unfortunately, it is very inefficient with zip files greater than about 1.5 megs with al of the overhead to uncompress then compress. What we need is a low level routine that actually reads a zip file, it's format and directory structures, to read and write uncompressed bytes. ZIp file format are out there somewhere, but be careful, UNIX and Windows/NT formats are different. But anyway, the following code should work (yeah, right):
[javajcode]
public class ZipAppendWriter {
ZipInputStream zis;
ZipOutputStream zos;
ZipEntry zeIn, zeOut;
File fInfile;
File fOutfile;
String strNewZipFilePath = null;
boolean bInputZipOK = false;
public ZipAppendWriter(String strZipFilePath) throws IOException
{
byte[] buf;
int iBufLength;
strNewZipFilePath = strZipFilePath;
// Open output
fOutfile = new File(System.currentTimeMillis() +"temp"); // Unique temp filename
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(fOutfile)));
zos.setMethod(ZipOutputStream.DEFLATED);
// Open input zip file and read to EOF
fInfile = new File(strZipFilePath);
if (fInfile.isFile())
{
zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(fInfile)));
bInputZipOK = true;
while ((zeIn = zis.getNextEntry()) != null)
{
System.out.println("ZE name= " +zeIn.getName() +" size= " +zeIn.getSize());
//buf = new byte[(int) zeIn.getSize()];
buf = new byte[5000000];
// Read the zip entry data
while ((iBufLength = zis.read(buf)) >= 0);
// Write the zip entry to temp output
zeOut = new ZipEntry(zeIn.getName());
zeOut.setSize((long) zeIn.getSize());
System.out.println("ZE name= " +zeIn.getName() +" out setsize= " +zeIn.getSize());
System.out.println(zeIn.getName());
zos.putNextEntry(zeOut);
zos.write(buf, 0, (int) zeIn.getSize());
}
zis.close();
}
}
public void write(byte[] baData,
int iOffset,
int iLength,
String strEntryName) throws IOException
{
zeOut = new ZipEntry(strEntryName);
zeOut.setSize((long) iLength);
System.out.println("ZE Setsize() to " + iLength);
zos.putNextEntry(zeOut);
zos.write(baData, iOffset, iLength);
}
public void close() throws IOException
{
// Delete the original file
if (bInputZipOK)
fInfile.delete();
// Rename temp to original filename
zos.close();
fOutfile.renameTo(new File(strNewZipFilePath));
}
public static void main (String args[])
{
String s = new String("My dog barks then runs away");
long t0 = System.currentTimeMillis();
try
{
eSrvZipAppendWriter zfw = new eSrvZipAppendWriter("C:\\test\\unbind.zip");
zfw.write(s.getBytes(), 0, s.length(), "Dogbarks");
zfw.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
long t1 = System.currentTimeMillis();
System.out.println("Zip append took " +(t1 - t0) +" milliseconds");
}
}
[/javacode]
d.moeller
October 20th, 1999, 04:33 PM
Yep, I've solved it this way by now. Thanks a bunch anyway!
yhan888
April 25th, 2000, 07:56 PM
you can just call Runtime.exec("jar ..."). It is robust.