Java - Pin existing files

Possible duplicate:
Adding files to a zip file using Java

Hi, Java developers,

Here's the script:

Let's say I have a text file called sample.txt . I really want to make a sample.txt file in a *.zip file called TextFiles.zip .

Here is 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 still creates the *.zip file and inserts the sample.txt file.
My question is: how can I insert an existing file into the created *.zip file?
If your answer has anything to do with TrueZip , send an SSCCE message. I have done the following:

  • Googled
  • Find an existing question. (Several found. No answer. Some did not answer my specific question.
  • Read TrueZip . But I couldn’t understand anything. (Please understand)
+4
source share
3 answers

Using the built-in Java API. This will add the file to the Zip file, it will replace any existing Zip files that may exist by 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) { } } } } 
+7
source

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

+3
source

It looks like according to the JDK epic link , you can use the while zis.getNextEntry() != null loop to cycle through the file (where zis is ZipInputStream), then use zis.read() to read into the array that is sent to ArrayList or similar.

Then one could use toArray() , "pass" it to the byte array using this method, and zos.write() into it the output ZIP file (where zos is ZipOutputStream ), using zos.putNextEntry() to create new records. (You will need to save ZipEntry and get its name with ze.getName() , and ze will be ZipEntry .) You should replace T with byte and byte (use byte everywhere, but for body of the loop) and you may need to change the cast code, to use Byte.byteValue() to convert from byte (wrapper class) to byte (primitive type), for example:

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

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

Sorry if this was a little tight and hope this helps!

+1
source

All Articles