Compress, Zip a stream which is not a file

Discussion in 'Java' started by helene.bh, Mar 16, 2007.

  1. helene.bh

    helene.bh Guest

    Hello,

    I need to compress a ByteArrayOuputStream (PDF) without saving it on
    the disk, so I need to zip a stream which won't be convert as a file.
    Do you know if it is possible ?

    thx !
    helene.bh, Mar 16, 2007
    #1
    1. Advertising

  2. Hi,

    helene.bh wrote:
    > Hello,
    >
    > I need to compress a ByteArrayOuputStream (PDF) without saving it on
    > the disk, so I need to zip a stream which won't be convert as a file.
    > Do you know if it is possible ?
    >
    > thx !


    Of course. Take a look at
    jdk1.5.0-docs\api\java\util\zip\package-summary.html

    Ciao,
    Ingo
    Ingo R. Homann, Mar 16, 2007
    #2
    1. Advertising

  3. helene.bh

    helene.bh Guest

    Hi Ingo,

    I see how to create a zip thx to a zipentry object which is created
    from a file, but I don't understand how to do it from a different
    stream, can u help me with that ? thx
    helene.bh, Mar 16, 2007
    #3
  4. Hi helene,

    helene.bh wrote:
    > Hi Ingo,
    >
    > I see how to create a zip thx to a zipentry object which is created
    > from a file, but I don't understand how to do it from a different
    > stream, can u help me with that ? thx


    ZipOutputStream zip=new ZipOutputStream(...);
    zip.putNextEntry(new ZipEntry(name));
    zip.write(bs,0,bs.length);
    zip.close();

    What exactly do you not understand?

    Ciao,
    Ingo
    Ingo R. Homann, Mar 16, 2007
    #4
  5. helene.bh

    helene.bh Guest

    On 16 Mar, 13:45, "Ingo R. Homann" <> wrote:
    > Hi helene,
    >
    > helene.bh wrote:
    > > Hi Ingo,

    >
    > > I see how to create a zip thx to a zipentry object which is created
    > > from a file, but I don't understand how to do it from a different
    > > stream, can u help me with that ? thx

    >
    > ZipOutputStream zip=new ZipOutputStream(...);
    > zip.putNextEntry(new ZipEntry(name));
    > zip.write(bs,0,bs.length);
    > zip.close();
    >
    > What exactly do you not understand?
    >
    > Ciao,
    > Ingo



    In the code you just gave, the ZipEntry constructor takes as argument
    the file name, doesn't it? but I need to compress a stream which is
    not a file it will be a stream i just created and that I need to
    compress and send right away.
    helene.bh, Mar 16, 2007
    #5
  6. On Mar 17, 1:41 am, "helene.bh" <> wrote:
    ...
    > ...the ZipEntry constructor takes as argument
    > the file name, doesn't it?


    A 'string' that might represent the name..
    <http://java.sun.com/javase/6/docs/api/java/util/zip/
    ZipEntry.html#constructor_summary>
    (if the bytes do not have a name, make one up!)

    Andrew T.
    Andrew Thompson, Mar 16, 2007
    #6
  7. helene.bh

    helene.bh Guest

    On 16 Mar, 14:47, "Andrew Thompson" <> wrote:
    > On Mar 17, 1:41 am, "helene.bh" <> wrote:
    > ..
    >
    > > ...the ZipEntry constructor takes as argument
    > > the file name, doesn't it?

    >
    > A 'string' that might represent the name..
    > <http://java.sun.com/javase/6/docs/api/java/util/zip/
    > ZipEntry.html#constructor_summary>
    > (if the bytes do not have a name, make one up!)
    >
    > Andrew T.


    Hello Andrew,

    I don't think the string is any name, I think this is the file name
    you want to compress.
    I tested it and if I change the string value i get a zip file with a
    wrong file inside.
    helene.bh, Mar 16, 2007
    #7
  8. helene.bh

    Chris Uppal Guest

    helene.bh wrote:

    > In the code you just gave, the ZipEntry constructor takes as argument
    > the file name, doesn't it? but I need to compress a stream which is
    > not a file it will be a stream i just created and that I need to
    > compress and send right away.


    Do you want to create a ZIP-format archive at all ? If not then chances are
    that either:
    http://java.sun.com/javase/6/docs/api/java/util/zip/DeflaterOutputStream.html
    or:
    http://java.sun.com/javase/6/docs/api/java/util/zip/DeflaterOutputStream.html
    is what you want.

    All those classes (including ZipOutputStream) write to a java.io,OutputStream,
    which can point to memory, or an external file, or anything else. See the
    JavaDoc.

    -- chris
    Chris Uppal, Mar 16, 2007
    #8
  9. helene.bh

    Nigel Wade Guest

    helene.bh wrote:

    > Hello,
    >
    > I need to compress a ByteArrayOuputStream (PDF) without saving it on
    > the disk, so I need to zip a stream which won't be convert as a file.
    > Do you know if it is possible ?
    >
    > thx !


    If all you want is a compressed stream then gzip format may be better. You can
    use GZIPOutputStream, which can wrap any OutputStream, to send the data. This
    stream can be uncompressed by the complementary GZIPInputStream.

    A Zip stream consists of compressed files in an archive container. It can be
    streamed, but might not be the most appropriate format if all you want is to
    compress some data in transit between two end points (a Socket perhaps?).

    --
    Nigel Wade, System Administrator, Space Plasma Physics Group,
    University of Leicester, Leicester, LE1 7RH, UK
    E-mail :
    Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
    Nigel Wade, Mar 16, 2007
    #9
  10. helene.bh

    Daniel Pitts Guest

    On Mar 16, 5:36 am, "helene.bh" <> wrote:
    > Hello,
    >
    > I need to compress a ByteArrayOuputStream (PDF) without saving it on
    > the disk, so I need to zip a stream which won't be convert as a file.
    > Do you know if it is possible ?
    >
    > thx !


    I was under the impression that a PDF is already compressed with a
    similar algorithm to ZIP compression. You get very little benefit
    from compressing a compressed file (often times you can only make it
    bigger)

    Anyway, Many people have given you the tools you need. ZipEntry indeed
    specifies a "file name". But that is the "file name" in the .zip file
    itself. You add the name, and then add the data associated with that
    name.

    Just to clarify, a .zip file contains a set of compressed "files"...
    You can compress arbitrary data in a .zip file, but you need to
    specify a name for it to work.

    Alternatively, as Nigel Wade suggested, you can use the GZIP input and
    output streams. GZIP compression does *not* care about "files". It
    is probably more appropriate for what you are doing... It is also
    supported in http. The most popular browsers can automatically
    uncompress a gzipped response.
    Daniel Pitts, Mar 16, 2007
    #10
  11. helene.bh

    Chris Uppal Guest

    Daniel Pitts wrote:

    > I was under the impression that a PDF is already compressed with a
    > similar algorithm to ZIP compression. You get very little benefit
    > from compressing a compressed file (often times you can only make it
    > bigger)


    PDF supports a compressed format, but it isn't required. Most
    individually-produced PDFs do seem to use the compressed form, but I don't know
    what standalone PDF creation libraries do. (My impression is that many such
    libraries are freeware of one sort or another, and freeware authors have tended
    to steer clear of potentially patent-encumbered compression formats -- but I've
    only looked at one such).

    Even the compressed format is relatively redundant -- I tried a quick
    experiment and gzipped a hundred or so randomly selected PDFs (actually the
    contents of my current "papers waiting to be read someday" directory ;-) and
    gzip compressed them by an average of about 10% each.

    -- chris
    Chris Uppal, Mar 17, 2007
    #11
  12. Chris Uppal wrote:
    > Even the compressed format is relatively redundant -- I tried a quick
    > experiment and gzipped a hundred or so randomly selected PDFs (actually the
    > contents of my current "papers waiting to be read someday" directory ;-) and
    > gzip compressed them by an average of about 10% each.


    There is a great deal stuff in the PDF format that is not compressed
    even in the "compressed stuff", because the basic page structure is not
    compressed, as well as the entire xref table (imagine how much
    compression that could give!)
    Joshua Cranmer, Mar 17, 2007
    #12
  13. helene.bh

    Guest

    On 3ÔÂ16ÈÕ, ÏÂÎç8ʱ36·Ö, "helene.bh" <> wrote:
    > Hello,
    >
    > I need to compress a ByteArrayOuputStream (PDF) without saving it on
    > the disk, so I need to zip a stream which won't be convert as a file.
    > Do you know if it is possible ?
    >
    > thx !


    More see here!
    http://www.flash50.com/index.php
    , Mar 18, 2007
    #13
  14. helene.bh

    Lew Guest

    wrote:
    > More see here!


    That is either missing a comma or it's an exclamation to a bishop that his
    purview extends farther than previously thought.

    > http://www.spam...50.com/index.php


    Spam! Spam! Spam! Spam!

    -- Lew
    Lew, Mar 18, 2007
    #14
  15. Hi,

    helene.bh wrote:
    > In the code you just gave, the ZipEntry constructor takes as argument
    > the file name, doesn't it? but I need to compress a stream which is
    > not a file it will be a stream i just created and that I need to
    > compress and send right away.


    In addition to what Chris said:

    The String is not more than a 'name'. It is secondary what e.g. a
    program like WinZip does with that name. You have a Stream, it is zipped
    and it can have different entries with different names. Not more and not
    less.

    Ciao,
    Ingo
    Ingo R. Homann, Mar 19, 2007
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?15nXldeg15kg15LXldec15PXkdeo15I=?=

    Do you know good zip (compress files) control?

    =?Utf-8?B?15nXldeg15kg15LXldec15PXkdeo15I=?=, May 23, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    471
    Kevin Spencer
    May 24, 2005
  2. could ildg
    Replies:
    0
    Views:
    292
    could ildg
    Jun 28, 2005
  3. http://edealseek.com/newsgroup.html

    Compress/Zip a String

    http://edealseek.com/newsgroup.html, Sep 12, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    107
    Malcolm Dew-Jones
    Sep 14, 2003
  4. Twig
    Replies:
    1
    Views:
    120
    A. Sinan Unur
    Jan 27, 2006
  5. Bo Yang
    Replies:
    9
    Views:
    286
    -berlin.de
    Nov 20, 2006
Loading...

Share This Page