Results 1 to 6 of 6
  1. #1
    prasannadavid is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default How to read multiple ZipEntry using single InputStream?

    I have to get a InputStream for a file from a File Server and hand that InputStream over to another server which process the InputStream.

    My problem is, some of the files are Zip files with multiple ZipEntry in them. I can do like ZipInputStream zip = new ZipInputStream(inputStream); and then do zip.getNextEntry() for each entry and read. But, my receiving server expect a single inputStream and I have to provide all zip entries in a single stream.

    Please let me know what to do or how to do. I cannot use ZipFile since the file is not in the same machine. I also cannot read and keep the whole file in a String or ByteArrayOutputStream as the contents of the files can be very big and it will fill up the memory.

    Is it possible to overload(extend) ZipInputStream in such a way that read() gives a single inputStream combining all the zip entries inside ? If so, please let me know how to do that.

    Thanks in advance,
    Prasanna

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Is it possible to overload(extend) ZipInputStream
    Have you looked at the API doc? Does it say the class is final? It it isn't you can extend it.
    Where will you insert your code to read the individual entries?

    some of the files are Zip files
    Your code should read the zip file to a local file and then extract each entry and pass that on.

    combining all the zip entries inside
    The combination would be a concatenation of all the files into a single file?

  3. #3
    prasannadavid is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi,
    Thankyou very much for your response.

    Your code should read the zip file to a local file and then extract each entry and pass that on.
    I am not supposed to create any local file in the server where the process runs. Files can be written only in to file server.


    The combination would be a concatenation of all the files into a single file?
    Exactly. The resultant inputStream must be a concatenation of all files(zipEntries) in the Zip into a single inputStream. For the receiving server, it should look like a single file.


    Where will you insert your code to read the individual entries?
    Since I am inexperienced in Java, I am having trouble here. I would basically want to change the behaviour of read(byte[] b, int off, int len) since that is what the receiving server will call. My read should keep on reading from each ZipEntry as far as required and return -1 when there are no more ZipEntries.

    Please suggest me.

    Thanks in advance,
    Prasanna

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Your code would need to read and save the "header" information that defines what is in the rest of the file. Then it would need to find and decompress each "entry" in the stream and return that to the receiving server. I guess your code would have to know the structure of the header and be able to extract what is needed to identify where in the stream each "entry" was. That would normally be done by the Zip handing classes if the file were saved to disk. But since it can't be, your code will have to do that while reading the stream.

  5. #5
    prasannadavid is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    Hi Norm,

    Thankyou very much again.

    Your code would need to read and save the "header" information that defines what is in the rest of the file. Then it would need to find and decompress each "entry" in the stream and return that to the receiving server. I guess your code would have to know the structure of the header and be able to extract what is needed to identify where in the stream each "entry" was.
    If I extend ZipInputStream, in my read(), will it be possible for me to call super.read() for each ZipEntry and return that as a single stream ? Am I missing something ?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    will it be possible
    I've never done it so the best I can recommend is to write a test program and see what happens.

    Reading the API doc, the only class I see that has methods to return a Stream is ZipFile.
    Which means you'd have to extend the File class to provide redirection for the input to come from the inputStream you are reading from.
    Last edited by Norm; 09-08-2010 at 03:12 PM. Reason: API doc refs

Similar Threads

  1. Multiple Sockets on a single Port
    By Sajith in forum Networking
    Replies: 2
    Last Post: 08-16-2010, 06:31 PM
  2. Multiple Java in a single BOX
    By luke_devon in forum New To Java
    Replies: 2
    Last Post: 07-05-2010, 01:59 PM
  3. multiple users with single connection
    By nishi.kishore in forum Java Servlet
    Replies: 3
    Last Post: 09-06-2009, 03:15 PM
  4. Unable to read data from inputstream
    By renuka_anil in forum Java Servlet
    Replies: 0
    Last Post: 01-29-2009, 03:20 PM
  5. Replies: 0
    Last Post: 12-22-2008, 06:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •