Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Compress, Zip a stream which is not a file

Thread view: 
helene.bh - 16 Mar 2007 13:36 GMT
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 !
Ingo R. Homann - 16 Mar 2007 13:39 GMT
Hi,

> Hello,
>
[quoted text clipped - 3 lines]
>
> thx !

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

Ciao,
Ingo
helene.bh - 16 Mar 2007 13:47 GMT
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
Ingo R. Homann - 16 Mar 2007 14:45 GMT
Hi helene,

> 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
helene.bh - 16 Mar 2007 15:41 GMT
> Hi helene,
>
[quoted text clipped - 13 lines]
> 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.
Andrew Thompson - 16 Mar 2007 15:47 GMT
..
> ...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.
helene.bh - 16 Mar 2007 16:07 GMT
> ..
>
[quoted text clipped - 7 lines]
>
> 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.
Chris Uppal - 16 Mar 2007 16:12 GMT
> 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
Ingo R. Homann - 19 Mar 2007 09:16 GMT
Hi,

> 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
Nigel Wade - 16 Mar 2007 16:43 GMT
> Hello,
>
[quoted text clipped - 3 lines]
>
> 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?).

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Daniel Pitts - 16 Mar 2007 22:00 GMT
> Hello,
>
[quoted text clipped - 3 lines]
>
> 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.
Chris Uppal - 17 Mar 2007 19:57 GMT
> 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
Joshua Cranmer - 17 Mar 2007 20:43 GMT
> 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!)
fy4.net@gmail.com - 18 Mar 2007 09:26 GMT
> Hello,
>
[quoted text clipped - 3 lines]
>
> thx !

More see here!
http://www.flash50.com/index.php
Lew - 18 Mar 2007 16:14 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.