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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.