Author Topic: ZipFile - adding an InputStream  (Read 14978 times)

Tom

  • Newbie
  • *
  • Posts: 20
    • View Profile
ZipFile - adding an InputStream
« on: April 18, 2011, 05:34:56 PM »
Could you include something like following?

zipFile.addFile(String entryPath, InputStream content, ZipParameters params)

Thx for considering,
Tom

admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 403
    • View Profile
Re: ZipFile - adding an InputStream
« Reply #1 on: April 21, 2011, 09:42:29 AM »
Good idea. I will look into it and will try to include this in the next release.

Tom

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: ZipFile - adding an InputStream
« Reply #2 on: May 16, 2011, 03:02:45 PM »
Any news about this request?

admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 403
    • View Profile
Re: ZipFile - adding an InputStream
« Reply #3 on: May 16, 2011, 10:04:21 PM »
Its a bit complicated to implement than I originally thought. I am trying my best to implement it as quickly as I can.

I need some clarification from you:

zipFile.addFile(String entryPath, InputStream content, ZipParameters params)

what exactly does "entryPath" mean above? Is it the the file path + file name (ex: c:\myfolder\myfile.txt)? And this file will be existing right?

Tom

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: ZipFile - adding an InputStream
« Reply #4 on: May 17, 2011, 09:46:12 AM »
entryPath should be the path (and filename) within the zip file - it does not exist on a file system, as i want to create it on the fly.
The content (InputStream) contains the data  - this can be a FileInputStream but alsa a ByteArrayInputStream,...

example:
Code: [Select]
ByteArrayInputStream content = new ByteArrayInputStream("testtext".getBytes("UTF-8"));
zipFile.addFile("dir1/dir2/testfile.txt", content, null);

admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 403
    • View Profile
Re: ZipFile - adding an InputStream
« Reply #5 on: May 19, 2011, 08:57:08 PM »
I will try to implement it for the next release. Will give you an update over the weekend.

admin

  • Administrator
  • Sr. Member
  • *****
  • Posts: 403
    • View Profile
Re: ZipFile - adding an InputStream
« Reply #6 on: May 25, 2011, 09:50:31 PM »
This feature is included in today's release (v1.2.4) available on the downloads page.

Here's how you can add content from stream to zip:

Quote
               InputStream is = null;
      
      try {
         ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddStreamToZip.zip");
         
         
         ZipParameters parameters = new ZipParameters();
         parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
         
         // below two parameters have to be set for adding content to a zip file
         // directly from a stream
         
         // this would be the name of the file for this entry in the zip file
         parameters.setFileNameInZip("yourfilename.txt");
         
         // we set this flag to true. If this flag is true, Zip4j identifies that
         // the data will not be from a file but directly from a stream
         parameters.setSourceExternalStream(true);
         
         // For this example I use a FileInputStream but in practise this can be
         // any inputstream
         is = new FileInputStream("filetoadd.txt");
         
         // Creates a new entry in the zip file and adds the content to the zip file
         zipFile.addStream(is, parameters);
         
      } catch (Exception e) {
         e.printStackTrace();
      } finally {
         if (is != null) {
            try {
               is.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }

This same functionality has been demonstrated in AddStreamToZip.java in the examples package (also available for download on the downloads page).

Few notes on this functionality:
  • Standard zip encryption cannot be used with this functionality unless you know CRC of the stream to be added in advance. If this is the case, then this CRC has to be set in ZipParameters.setSourceFileCRC(int) before calling ZipFile.addStream() method. There are no limitations for AES encryption
  • Zip4j wont work in threaded mode with this method. i.e, setting ZipFile.setRunInThread() will not have any effect and this method will execute in the same thread and not in a different thread
  • InputStream will NOT be closed after content is added to the zip file

hudi1

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: ZipFile - adding an InputStream
« Reply #7 on: February 26, 2013, 05:06:27 PM »
Hi,

excellent job but but I have two question. why setSourceFileCRC need int instead of long ? I see in your code that you use CRC32 which gives you long value of crc which you convert to int. Second question why there is such crc control ? I have one input stream and is hard to calculate crc and use it as parameter in your method

 


Warning: Cannot modify header information - headers already sent in /home/content/66/5374066/html/zip4j/forum/Sources/Errors.php on line 346

Warning: Cannot modify header information - headers already sent in /home/content/66/5374066/html/zip4j/forum/Sources/Errors.php on line 347

Warning: Cannot modify header information - headers already sent in /home/content/66/5374066/html/zip4j/forum/Sources/Errors.php on line 348

Warning: Cannot modify header information - headers already sent in /home/content/66/5374066/html/zip4j/forum/Sources/Errors.php on line 351

Warning: Cannot modify header information - headers already sent in /home/content/66/5374066/html/zip4j/forum/Sources/Errors.php on line 352

Warning: Cannot modify header information - headers already sent in /home/content/66/5374066/html/zip4j/forum/Sources/Errors.php on line 353
Connection Problems

Connection Problems

Sorry, SMF was unable to connect to the database. This may be caused by the server being busy. Please try again later.