Adding to an existing zip file

Can I add to an existing zip file without first reading out its contents? I am using the ZipOutputStream class.
cherch0rAsked:
Who is Participating?
 
JodConnect With a Mentor Commented:
Don't think you can. I have this code from codeguru.com which will encapsulate the process for you and make it a bit easier:

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");
}
}
0
 
Jan LouwerensSoftware EngineerCommented:
yes you can
which compression program do you use?
(WinZip, PKZip, etc...)
0
 
cherch0rAuthor Commented:
Edited text of question.
0
 
mbormannCommented:
agree with Jod

thar's a deep black well ,well kin u jump in without a lookin ?Nah ,u gotta look before ya leeap.

same way here
:-)
0
All Courses

From novice to tech pro — start learning today.