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(zipFileName,true);
    } catch(Exception e) {fos = new FileOutputStream(zipFileName);}

    ZipOutputStream zos = new ZipOutputStream(fos);
    ZipEntry zEntry = new ZipEntry(msgID + ".txt");
    zEntry.setMethod(zos.STORED);
    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.

LVL 1
jkb2Asked:
Who is Participating?
 
a.marshConnect With a Mentor Commented:
There seems no obvious method, but my recommendation would be using ZipInputStream to get what is already there and then use the ZipOutputStream to generate the complete file.

I'll put something together for you - while I'm doing that though someone else might get a complete solution for you sooner.

:o)

Ant
0
 
objectsCommented:
Ant is correct, it's not supported by the Zip classes.

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 + " opened.");

      // 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(tempJarFile));

         // 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.getName() + " added.");
            }
            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(fileName)) {
                  // 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 + " closed.");

         // 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(jarFile);
         System.out.println(jarName + " updated.");
      }
   }
}




0
 
jkb2Author Commented:
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
0
 
a.marshCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.