jkb2
asked on
ZipOutputStream Question
I don't even know if this is possible but I would like to be able to re-open a Zip file and add new entries to it.
I hava routine written that creates the Zip File and writes the data but every time I reopen the file it seems to overwrite the 1 entry thats in the file.
Any Ideas....
My Code for generating the new file....
FileOutputStream fos = null;
try {
fos = new FileOutputStream(zipFileNa me,true);
} catch(Exception e) {fos = new FileOutputStream(zipFileNa me);}
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry zEntry = new ZipEntry(msgID + ".txt");
zEntry.setMethod(zos.STORE D);
zos.putNextEntry(zEntry);
zos.write(baos.toByteArray ());
zos.closeEntry();
zos.close();
Need a quick answer so I will pump the points up on this one.
I hava routine written that creates the Zip File and writes the data but every time I reopen the file it seems to overwrite the 1 entry thats in the file.
Any Ideas....
My Code for generating the new file....
FileOutputStream fos = null;
try {
fos = new FileOutputStream(zipFileNa
} catch(Exception e) {fos = new FileOutputStream(zipFileNa
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry zEntry = new ZipEntry(msgID + ".txt");
zEntry.setMethod(zos.STORE
zos.putNextEntry(zEntry);
zos.write(baos.toByteArray
zos.closeEntry();
zos.close();
Need a quick answer so I will pump the points up on this one.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am accepting your answer because it tells me what I wanted to know -- not a good answer but the right answer. I figured out the complete rewrite but it doesn't suit my needs for this project. Guess I will just use folders instead of zips...
Thanks
Thanks
Glad to have been of some help.
Sorry I didn't get back to you in time with the code - got sidetracked with my "real" job I'm afraid.
:o)
Ant
Sorry I didn't get back to you in time with the code - got sidetracked with my "real" job I'm afraid.
:o)
Ant
Found the following for adding an entry to a jar file which should be straight forward to modify to work on zip file (JarFile is simply a subclass of ZipFIle).
import java.io.*;
import java.util.*;
import java.util.zip.*;
import java.util.jar.*;
public class JarUpdate {
/**
* main()
*/
public static void main(String[] args) throws IOException {
// Get the jar name and entry name from the command-line.
String jarName = args[0];
String fileName = args[1];
// Create file descriptors for the jar and a temp jar.
File jarFile = new File(jarName);
File tempJarFile = new File(jarName + ".tmp");
// Open the jar file.
JarFile jar = new JarFile(jarFile);
System.out.println(jarName
// Initialize a flag that will indicate that the jar was updated.
boolean jarUpdated = false;
try {
// Create a temp jar file with no manifest. (The manifest will
// be copied when the entries are copied.)
Manifest jarManifest = jar.getManifest();
JarOutputStream tempJar =
new JarOutputStream(new FileOutputStream(tempJarFi
// Allocate a buffer for reading entry data.
byte[] buffer = new byte[1024];
int bytesRead;
try {
// Open the given file.
FileInputStream file = new FileInputStream(fileName);
try {
// Create a jar entry and add it to the temp jar.
JarEntry entry = new JarEntry(fileName);
tempJar.putNextEntry(entry
// Read the file and write it to the jar.
while ((bytesRead = file.read(buffer)) != -1) {
tempJar.write(buffer, 0, bytesRead);
}
System.out.println(entry.g
}
finally {
file.close();
}
// Loop through the jar entries and add them to the temp jar,
// skipping the entry that was added to the temp jar already.
for (Enumeration entries = jar.entries(); entries.hasMoreElements();
// Get the next entry.
JarEntry entry = (JarEntry) entries.nextElement();
// If the entry has not been added already, add it.
if (! entry.getName().equals(fil
// Get an input stream for the entry.
InputStream entryStream = jar.getInputStream(entry);
// Read the entry and write it to the temp jar.
tempJar.putNextEntry(entry
while ((bytesRead = entryStream.read(buffer)) != -1) {
tempJar.write(buffer, 0, bytesRead);
}
}
}
jarUpdated = true;
}
catch (Exception ex) {
System.out.println(ex);
// Add a stub entry here, so that the jar will close without an
// exception.
tempJar.putNextEntry(new JarEntry("stub"));
}
finally {
tempJar.close();
}
}
finally {
jar.close();
System.out.println(jarName
// If the jar was not updated, delete the temp jar file.
if (! jarUpdated) {
tempJarFile.delete();
}
}
// If the jar was updated, delete the original jar file and rename the
// temp jar file to the original name.
if (jarUpdated) {
jarFile.delete();
tempJarFile.renameTo(jarFi
System.out.println(jarName
}
}
}