how to append to zip files

I would like to know how to append to a zip file, I know how to write a zip file and I can put as many entries in the first time but if I run my program again and try to add a file to the same zip file it erases what was already in it. I use ZipOutputStream(new FileOutputStream), I have tried the FileOutputStream append but it does not seem to work for zip files, I have tried RandomAccessFile on my zip file but that just seems to add the bytes to the file that already in the zip file, and makes the file unopenable, corrupt. If you can help, please tell me how.

[580 byte] By [wslyhbb] at [2007-9-26 2:58:53]
# 1

You actually have to open the previous zipped file, read it's entries and their data and then rewrite a new zip file with these entries and their data. You can then delete your first file and rename the new one so the effect is that you have "appended" zip files to the existing archive.

Here is an example which I pieced together from a larger bit of code that I have. This would need to be in a try/catch but this should give you a start.

/** This method adds a new file to an existing zip file. It *includes all the zipped entries previously written.

*@param file The old zip archive

*@param filename The name of the file to be Added

*@param data The data to go into the zipped file

*NOTE: This method will write a new file and data to an archive, to

*write an existing file, we must first read the data frm the file,

*then you could call this method.

*/

public void addToArchive(File file, String filename, String data)

{

ZipOutputStream zipOutput = null;

ZipFile zipFile = null;

Enumeration zippedFiles = null;

ZipEntry currEntry = null;

ZipEntry entry = null;

zipFile = new ZipFile( file.getAbsolutePath() );

//get an enumeration of all existing entries

zippedFiles = zipFile.entries();

//create your output zip file

zipOutput = new ZipOutputStream( new FileOutputStream ( new File( "NEW" + file.getAbsolutePath() ) ) );

//Get all the data out of the previously zipped files and write it to a new ZipEntry to go into a new file archive

while (zippedFiles.hasMoreElements())

{

//Retrieve entry of existing files

currEntry = (ZipEntry)zippedFiles.nextElement();

//Read data from existing file

BufferedReader reader = new BufferedReader( new InputStreamReader( zipFile.getInputStream( currEntry ) ) );

String currentLine = null;

StringBuffer buffer = new StringBuffer();

while( (currentLine = reader.readLine() ) != null )

{

buffer.append( currentLine);

}

//Commit the data

zipOutput.putNextEntry(new ZipEntry(currEntry.getName()) ) ;

zipOutput.write (buffer.toString().getBytes() );

zipOutput.flush();

zipOutput.closeEntry();

}

//Close the old zip file

zipFile.close();

//Write the 'new' file to the archive, commit, and close

entry = new ZipEntry( newFileEntryName );

zipOutput.putNextEntry( entry );

zipOutput.write( data.getBytes() );

zipOutput.flush();

zipOutput.closeEntry();

zipOutput.close();

//delete the old file and rename the new one

File toBeDeleted = new File ( file.getAbsolutePath() );

toBeDeleted.delete();

File toBeRenamed = new File ( "NEW" + file.getAbsolutePath() );

toBeRenamed.rename( file );

}

Like I said I haven't tested this code as it is here but hopefully it can help. Good luck.

webst001 at 2007-6-29 10:54:18 > top of Java-index,Archived Forums,Java Programming...