Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Compress, Zip a stream which is not a file

Reply
Thread Tools

Compress, Zip a stream which is not a file

 
 
helene.bh
Guest
Posts: n/a
 
      03-16-2007
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 !

 
Reply With Quote
 
 
 
 
Ingo R. Homann
Guest
Posts: n/a
 
      03-16-2007
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



 
Reply With Quote
 
 
 
 
helene.bh
Guest
Posts: n/a
 
      03-16-2007
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

 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      03-16-2007
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

 
Reply With Quote
 
helene.bh
Guest
Posts: n/a
 
      03-16-2007
On 16 Mar, 13:45, "Ingo R. Homann" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      03-16-2007
On Mar 17, 1:41 am, "helene.bh" <(E-Mail Removed)> 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.

 
Reply With Quote
 
helene.bh
Guest
Posts: n/a
 
      03-16-2007
On 16 Mar, 14:47, "Andrew Thompson" <(E-Mail Removed)> wrote:
> On Mar 17, 1:41 am, "helene.bh" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      03-16-2007
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/ap...putStream.html
or:
http://java.sun.com/javase/6/docs/ap...putStream.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



 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      03-16-2007
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 : http://www.velocityreviews.com/forums/(E-Mail Removed)
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      03-16-2007
On Mar 16, 5:36 am, "helene.bh" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Possibility to add a zip-file to a new zip-file with "add to zip" (right-click) ?? erikkie@casema.nl Computer Support 4 06-26-2006 12:18 AM
Re: Add file to zip, or replace file in zip Chris Lambacher Python 0 06-01-2006 09:39 PM
Add file to zip, or replace file in zip majj81 Python 0 05-31-2006 07:10 AM
Add file to zip, or replace file in zip abcd Python 7 05-01-2006 10:19 PM
java.util.zip.ZipException: error in opening zip file hari_om@hotmail.com Java 0 05-03-2005 04:47 PM



Advertisments