Solved

ZipOutputStream Question

Posted on 2001-08-07
4
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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.
Suggested Courses

737 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