Java - Zipping existing files [duplicate]

大憨熊 提交于 2019-11-30 18:11:02

问题


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 )

回答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) {
      }
    }
  }
}



回答2:


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




回答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!!



来源:https://stackoverflow.com/questions/14350629/java-zipping-existing-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!