Article View: borland.public.jbuilder.java.language
Article #8589Re: ZipOutputStream question - please help!!!
From: Vladimir Patryshev
Date: Sat, 10 Mar 2001 02:55
Date: Sat, 10 Mar 2001 02:55
248 lines
9206 bytes
9206 bytes
--------------277543029C867707116D187E
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Hi,
You could probably make your code more efficient if you replace this:
BufferedReader in = new BufferedReader(new FileReader(files[i]));
int c;
while((c = in.read()) != -1)
out.write(c);
with something that does it in blocks.
Thanks,
Vlad
Yonatan Taub wrote:
> Hello!
>
> I've written a simple object which accepts a File[] array and compresses it
> with a ZipOutputStream object to a given OutputStream.
> It works fine. The only problem is when I try to compress about 12MB, it
> take about 50 seconds to complete while WinZip does it in 6 seconds.
>
> Does my code has a logical flaw? Do I need to do something with the
> buffering?
>
> The compressor object:
>
> import java.util.zip.*;
> import java.io.*;
>
> /**
> * This class compresses files with the zip format and writes the output to
> a specified OutputStream.
> */
>
> public class ZipCompressor {
>
> private ZipOutputStream out;
>
> /**
> * The constructor accepts an OutputStream object to which the data will
> be written to.
> *
> * @param outputStream - the outputstream to which the compressed files
> will be written.
> */
>
> public ZipCompressor(OutputStream outputStream) {
> out = new ZipOutputStream(new BufferedOutputStream(outputStream));
> }
>
> /**
> * The method accepts a File array containing the files to compress.
> *
> * @param files - a File array containing the desired files to compress.
> *
> * @throws FileNotFoundException - if a file in the array is not found.
> * @throws IOException - if an IOException occurs.
> */
>
> public void compressFiles(File[] files) throws FileNotFoundException,
> IOException {
> for(int i = 0; i < files.length; i++)
> {
> String fileName = files[i].getName();
>
> out.putNextEntry(new ZipEntry(fileName));
> BufferedReader in = new BufferedReader(new FileReader(files[i]));
>
> int c;
>
> while((c = in.read()) != -1)
> out.write(c);
>
> in.close();
>
> } // end for loop
> }
>
> /**
> * Writes the compressed data to the outputstream (which was specified in
> the constructor).
> *
> * @throws IOException - if the closing the stream fails.
> */
>
> public void write() throws IOException {
> out.flush();
> out.close();
> }
>
> } // end class ZipCompressor
>
> ============================================================================
> ============
> The main() :
>
> import java.io.*;
>
> public class Test {
>
> public static void main(String[] args) {
>
> File[] files = {
> new File("d:/java/tests/tests.jpr"),
> new File("D:/JBuilder4/jdk1.3/jre/lib/rt.jar"),
> };
>
> long start = System.currentTimeMillis();
>
> try {
> ZipCompressor compressor = new ZipCompressor(new
> FileOutputStream("d:/test.zip"));
> compressor.compressFiles(files);
> compressor.write();
> } catch(Exception x) { x.printStackTrace(); }
>
> long stop = System.currentTimeMillis();
>
> System.out.println("Total seconds: " + (stop - start) / 1000);
> }
>
> } // end class Test
>
> Thanks!!!!
--------------277543029C867707116D187E
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hi,
<p>You could probably make your code more efficient if you replace this:
<p><tt> BufferedReader in = new BufferedReader(new
FileReader(files[i]));</tt><tt></tt>
<p><tt> int c;</tt><tt></tt>
<p><tt> while((c = in.read()) != -1)</tt>
<br><tt> out.write(c);</tt><tt></tt>
<p>with something that does it in blocks.
<p>Thanks,
<br>Vlad
<p>Yonatan Taub wrote:
<blockquote TYPE=CITE>Hello!
<p>I've written a simple object which accepts a File[] array and compresses
it
<br>with a ZipOutputStream object to a given OutputStream.
<br>It works fine. The only problem is when I try to compress about 12MB,
it
<br>take about 50 seconds to complete while WinZip does it in 6 seconds.
<p>Does my code has a logical flaw? Do I need to do something with the
<br>buffering?
<p>The compressor object:
<p>import java.util.zip.*;
<br>import java.io.*;
<p>/**
<br> * This class compresses files with the zip format and writes
the output to
<br>a specified OutputStream.
<br> */
<p>public class ZipCompressor {
<p> private ZipOutputStream out;
<p> /**
<br> * The constructor accepts an OutputStream object to which
the data will
<br>be written to.
<br> *
<br> * @param outputStream - the outputstream to which the
compressed files
<br>will be written.
<br> */
<p> public ZipCompressor(OutputStream outputStream) {
<br> out = new ZipOutputStream(new BufferedOutputStream(outputStream));
<br> }
<p> /**
<br> * The method accepts a File array containing the files
to compress.
<br> *
<br> * @param files - a File array containing the desired files
to compress.
<br> *
<br> * @throws FileNotFoundException - if a file in the array
is not found.
<br> * @throws IOException - if an IOException occurs.
<br> */
<p> public void compressFiles(File[] files) throws FileNotFoundException,
<br>IOException {
<br> for(int i = 0; i < files.length; i++)
<br> {
<br> String fileName = files[i].getName();
<p> out.putNextEntry(new ZipEntry(fileName));
<br> BufferedReader in = new BufferedReader(new
FileReader(files[i]));
<p> int c;
<p> while((c = in.read()) != -1)
<br> out.write(c);
<p> in.close();
<p> } // end for loop
<br> }
<p> /**
<br> * Writes the compressed data to the outputstream
(which was specified in
<br>the constructor).
<br> *
<br> * @throws IOException - if the closing the stream
fails.
<br> */
<p> public void write() throws IOException {
<br> out.flush();
<br> out.close();
<br> }
<p>} // end class ZipCompressor
<p>============================================================================
<br>============
<br>The main() :
<p>import java.io.*;
<p>public class Test {
<p> public static void main(String[] args) {
<p> File[] files = {
<br>
new File("d:/java/tests/tests.jpr"),
<br>
new File("D:/JBuilder4/jdk1.3/jre/lib/rt.jar"),
<br>
};
<p> long start = System.currentTimeMillis();
<p> try {
<br> ZipCompressor compressor = new ZipCompressor(new
<br>FileOutputStream("d:/test.zip"));
<br> compressor.compressFiles(files);
<br> compressor.write();
<br> } catch(Exception x) { x.printStackTrace(); }
<p> long stop = System.currentTimeMillis();
<p> System.out.println("Total seconds: " + (stop - start)
/ 1000);
<br> }
<p>} // end class Test
<p>Thanks!!!!</blockquote>
</html>
--------------277543029C867707116D187E--
Technical Details
Message-ID:
<3AA99791.B186129B@borland.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mailReferences:
<3aa80218_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,f1b1ab5b0816f6b8 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-09 19:02:06 PST Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!newsfeed.wirehub.nl!PeerFeed.KWeb.on.ca!bornews.borland.com!not-for-mail From: Vladimir Patryshev <vpatryshev@borland.com> Newsgroups: borland.public.jbuilder.java.language Subject: Re: ZipOutputStream question - please help!!! Date: Fri, 09 Mar 2001 18:55:14 -0800 Organization: Borland Lines: 250 Message-ID: <3AA99791.B186129B@borland.com> References: <3aa80218_1@dnews> NNTP-Posting-Host: VLADGX1.inprise.com Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="------------277543029C867707116D187E" X-Mailer: Mozilla 4.72 [en] (WinNT; U) X-Accept-Language: en,ru,es-AR