Menu

Getting an InputStream on one of the Compress

Masca
2009-01-17
2013-05-28
  • Masca

    Masca - 2009-01-17

    Hi,

    I am not sure if this is the correct place to ask this question. If it is not, please point me to the correct place. 

    I am trying to use the Java SDK to be able to get the InputStream of a file within a 7Zip Archive. Is this possible? I looked at the sample code in SevenZip.J7zip.java – and I can get the name of the file – but I cannot find a way to get its InputStream. I do not want to extract this file as it would be too large. 

    Anyway how to get the InputStream without extracting the File?

    Actually, I have a 7Zip of a tar. The tar itself contains a large number of files. I would like to access these files individually without uncompressing. I have code to access individual files from the Tar, if I get the InputStream from 7Zip without decompressing the tar ball. Anyway to do this?

    I hope I was clear in the above explanation. If not, let me know.

    Thanks to anyone who helps.
    Regards,

     
    • my space

      my space - 2009-01-18

      Hi,

      (sorry for my english ...)

      Did you read the readme.txt file provided by J7zip_4.43_alpha2.zip ?

      Here :
        Please read SevenZip\J7zip.java and SevenZip\ArchiveExtractCallback.java as samples.

         the public API of J7Zip.jar is :
          SevenZip.Archive.SevenZip.Handler
          SevenZip.Archive.SevenZipEntry
          SevenZip.Archive.IArchiveExtractCallback
          SevenZip.Archive.IInArchive
         

         
      Remark : the Java SDK follows the same rules/principle as the C++ SDK.
      This principle does not follow the Java principle :(

      For your case :
        You must find an java objet that have the InputStream interface and the OutputStream interface  !
      (one thread writes into this object, and another thread reads the data ...)
       
        then look at SevenZip\J7zip.java to extract a file :
          MyRandomAccessFile istream = new MyRandomAccessFile(filename,"r");
             
          IInArchive archive = new Handler();
             
          int ret = archive.Open( istream );
         
          testOrExtract(archive,listOfNames,IInArchive.NExtract_NAskMode_kExtract); 

          archive.close();
         
         
        In  testOrExtract, you must use your own "ArchiveExtractCallback" :
           ArchiveExtractCallback extractCallbackSpec = new ArchiveExtractCallback();
           becomes :
           MyArchiveExtractCallback extractCallbackSpec = new MyArchiveExtractCallback();
           (and perhaps : new MyArchiveExtractCallback(myInputOutputStreamObjet);
          
          
        then look at SevenZip\ArchiveExtractCallback.java as a sample to write you own MyArchiveExtractCallback.java
        look at the  "GetStream" method :
           java.io.RandomAccessFile outStr = new java.io.RandomAccessFile(_filePath,"rw");
                         
            if (pos != -1) {
                outStr.seek(pos);
            }
           
            outStream[0] = new OutputStream(outStr);
       
        
         Replace "OutputStream" with your own class (with the InputStream and OutputStream interface)
        
        
      The 7-Zip principle :
        7-Zip asks for an "java.io.OutputStream" when it wants to write a file.
        (sometimes, it wants to add data to an existing file, I not sure why,
         perhaps the data was in two blocks in a solid archive ?).
       
        Then 7-Zip writes, by its own, the data in this "java.io.OutputStream" object ...
       
      Your principle :
        Your "OutputStream" objet must store data in memory each time 7-zip calls "write" method.
        If the internal buffer of "OutputStream" is full, this object must lock.
       
        Your "OutputStream" objet can read data from this internal buffer or lock when there is no data ...
       
      Remark : so you must have two threads :
      - one for reading the "OutputStream" (create your myInputOutputStreamObjet, launch the second thread, then read ...)
      - one for writing the "OutputStream" (i.e. for the 7-zip SDK, open the archive, launch the extracting procedure ...)
      ...

      I hope you understand this not so easy explanation ;) 
        
        
      Remark : I don't like the 7-zip principle but I think that nobody can develop efficiently
      the "Java principle" with solid archives ...

       
      • Masca

        Masca - 2009-01-18

        Dear MySpace,

            Thanks for taking the time to write this long post.

            I understand that you are suggesting a Producer/Consumer Model – and this is something that I did not think about. Thanks for that. I think I would have to check with others at work, if I really should be going along this route.

            I appreciate you pointing out that the stream can be got from ArchiveExtractCallback. I had looked into J7Zip and Handler, and ignored the ArchiveExtractCallback thinking it is only to provide progress information.

        Thanks again. I need to think about this.

         

Log in to post a comment.