Solved

ZipOutputStream Question

Posted on 2001-08-07
4
469 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 66
java set up 1 44
eclipse package explorer vs project explorer view 2 27
maven project error 5 17
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

771 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

7 Experts available now in Live!

Get 1:1 Help Now