Compress, Zip a stream which is not a file

H

helene.bh

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

Ingo R. Homann

Hi,

helene.bh said:
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
 
H

helene.bh

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
 
I

Ingo R. Homann

Hi helene,

helene.bh said:
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
 
H

helene.bh

Hi helene,




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.
 
C

Chris Uppal

helene.bh said:
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
 
N

Nigel Wade

helene.bh said:
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?).
 
D

Daniel Pitts

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.
 
C

Chris Uppal

Daniel said:
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
 
J

Joshua Cranmer

Chris said:
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!)
 
I

Ingo R. Homann

Hi,

helene.bh said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top