Solved

ZipOutputStream Question

Posted on 2001-08-07
4
478 Views
Last Modified: 2006-11-17
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.

0
Comment
Question by:jkb2
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
a.marsh earned 200 total points
ID: 6361319
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
 
LVL 92

Expert Comment

by:objects
ID: 6362105
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
 
LVL 1

Author Comment

by:jkb2
ID: 6364315
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
 
LVL 15

Expert Comment

by:a.marsh
ID: 6364368
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now