Java - Zipping existing files

13,352

Solution 1

Using the inbuilt Java API. This will add a file to a Zip File, this will replace any existing Zip files that may exist, creating a new Zip file.

public class TestZip02 {

  public static void main(String[] args) {
    try {
      zip(new File("TextFiles.zip"), new File("sample.txt"));
    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public static void zip(File zip, File file) throws IOException {
    ZipOutputStream zos = null;
    try {
      String name = file.getName();
      zos = new ZipOutputStream(new FileOutputStream(zip));

      ZipEntry entry = new ZipEntry(name);
      zos.putNextEntry(entry);

      FileInputStream fis = null;
      try {
        fis = new FileInputStream(file);
        byte[] byteBuffer = new byte[1024];
        int bytesRead = -1;
        while ((bytesRead = fis.read(byteBuffer)) != -1) {
          zos.write(byteBuffer, 0, bytesRead);
        }
        zos.flush();
      } finally {
        try {
          fis.close();
        } catch (Exception e) {
        }
      }
      zos.closeEntry();

      zos.flush();
    } finally {
      try {
        zos.close();
      } catch (Exception e) {
      }
    }
  }
}

Solution 2

Here you can get answer for your question: http://truezip.schlichtherle.de/2011/07/26/appending-to-zip-files/

Solution 3

It seems that, according to the epic JDK reference, you could use a while zis.getNextEntry() != null loop to loop through the file (where zis is a ZipInputStream), then use zis.read() to read into an array, which is sent to an ArrayList or similar.

Then, one could use toArray(), "cast" it to a byte array with this method and zos.write() it into the output ZIP file (where zos is a ZipOutputStream), using zos.putNextEntry() to make new entries. (You will need to save the ZipEntry and get its name with ze.getName(), with ze being a ZipEntry.)You should replace T with Byte and byte (use byte everywhere but the for loop body) and may need to modify the casting code to use Byte.byteValue() to convert from Byte (wrapper class) to byte (primitive type), like so:

for(int i = 0; i < objects.length; i++) {
    convertedObjects[i] = (Byte)objects[i].byteValue();
}

Note that this is untested and based on the JDK (entries ZipInputStream, ZipOutputStream, ArrayList, and Byte) and a Google search on array casting.

Sorry if that was a bit dense, and hope this helps!!

Share:
13,352

Related videos on Youtube

Michael 'Maik' Ardan
Author by

Michael 'Maik' Ardan

Henlo

Updated on July 15, 2022

Comments

  • Michael 'Maik' Ardan
    Michael 'Maik' Ardan almost 2 years

    Possible Duplicate:
    Appending files to a zip file with Java

    Hello Java Developers,

    Here's the scenario:

    Say I have a textfile named sample.txt. What I actually want to do is to put the sample.txt file into a *.zip file named TextFiles.zip.

    Here's what I have learned so far.

    try{
        File f = new File(compProperty.getZIP_OUTPUT_PATH());
        zipOut = new ZipOutputStream(new FileOutputStream(f));
        ZipEntry zipEntry = new ZipEntry("sample.txt");
        zipOut.putNextEntry(zipEntry);
        zipOut.closeEntry();
        zipOut.close();
        System.out.println("Done");
    
    } catch ( Exception e ){
        // My catch block
    }
    

    My code so far creates a *.zip file and insert the sample.txt file.
    My question is how would I be able to insert an existing file to the created *.zip file?
    If your answer has anything to do with TrueZIP, please post an SSCCE.

    I have done the following:

    • Googled
    • Search for existing question. ( Found few. No answer. Some didn't answer my particular question.
    • Read TrueZip. Yet, I couldn't understand a thing. ( Please do understand )
    • MadProgrammer
      MadProgrammer over 11 years
      1- You example scares me, as you are not actually coping any data to the zip file, just preparing an index. 2- Zip doesn't have a concept of "append". Append is normally achieved by copying the contents of the existing Zip file to a new Zip file, adding in the new content, deleting the old Zip file and renaming the new one back into its place. You can take a look at this and this for some examples
  • Michael 'Maik' Ardan
    Michael 'Maik' Ardan over 11 years
    Thanks! This was a great link. I'll take a look on this.
  • Pshemo
    Pshemo over 11 years
    +1 but remember that it is good practice to provide most relevant part of link in answer, in case the target site is unreachable or goes permanently offline. Please read How to Answer.
  • Michael 'Maik' Ardan
    Michael 'Maik' Ardan over 11 years
    Great! As expected from you. Thank you! This is what exactly I was looking for.
  • Michael 'Maik' Ardan
    Michael 'Maik' Ardan over 11 years
    This actually answered my question. However, for my learning, what do the try block under FileInputStream fis = null; does?
  • MadProgrammer
    MadProgrammer over 11 years
    Basic a Zip is split into 2 sections, entries and data. Entries describe the data blocks, including where to find them, so, once your added an entry, it opens a block for you to write to. The secion you are talking about basically uses a FileInputStream to read the file contents and pass it through the ZipOutputStream which will compress the file contents and add it to the data block of the current entry. Once done, I flush the ZipOutputStream (because I'm paranoid) and close of the entry.