🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8589

Re: ZipOutputStream question - please help!!!

#8589
From: Vladimir Patryshev
Date: Sat, 10 Mar 2001 02:55
248 lines
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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BufferedReader in = new BufferedReader(new
FileReader(files[i]));</tt><tt></tt>
<p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int c;</tt><tt></tt>
<p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while((c = in.read()) != -1)</tt>
<br><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;* This class compresses files with the zip format and writes
the output to
<br>a specified OutputStream.
<br>&nbsp;*/
<p>public class ZipCompressor {
<p>&nbsp; private ZipOutputStream out;
<p>&nbsp; /**
<br>&nbsp;&nbsp; * The constructor accepts an OutputStream object to which
the data will
<br>be written to.
<br>&nbsp;&nbsp; *
<br>&nbsp;&nbsp; * @param outputStream - the outputstream to which the
compressed files
<br>will be written.
<br>&nbsp;&nbsp; */
<p>&nbsp; public ZipCompressor(OutputStream outputStream) {
<br>&nbsp;&nbsp;&nbsp; out = new ZipOutputStream(new BufferedOutputStream(outputStream));
<br>&nbsp; }
<p>&nbsp; /**
<br>&nbsp;&nbsp; * The method accepts a File array containing the files
to compress.
<br>&nbsp;&nbsp; *
<br>&nbsp;&nbsp; * @param files - a File array containing the desired files
to compress.
<br>&nbsp;&nbsp; *
<br>&nbsp;&nbsp; * @throws FileNotFoundException - if a file in the array
is not found.
<br>&nbsp;&nbsp; * @throws IOException - if an IOException occurs.
<br>&nbsp;&nbsp; */
<p>&nbsp;&nbsp; public void compressFiles(File[] files) throws FileNotFoundException,
<br>IOException {
<br>&nbsp;&nbsp;&nbsp;&nbsp; for(int i = 0; i &lt; files.length; i++)
<br>&nbsp;&nbsp;&nbsp;&nbsp; {
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String fileName = files[i].getName();
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.putNextEntry(new ZipEntry(fileName));
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BufferedReader in = new BufferedReader(new
FileReader(files[i]));
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int c;
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while((c = in.read()) != -1)
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.write(c);
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in.close();
<p>&nbsp;&nbsp;&nbsp;&nbsp; } // end for loop
<br>&nbsp;&nbsp; }
<p>&nbsp;&nbsp; /**
<br>&nbsp;&nbsp;&nbsp; * Writes the compressed data to the outputstream
(which was specified in
<br>the constructor).
<br>&nbsp;&nbsp;&nbsp; *
<br>&nbsp;&nbsp;&nbsp; * @throws IOException - if the closing the stream
fails.
<br>&nbsp;&nbsp;&nbsp; */
<p>&nbsp;&nbsp; public void write() throws IOException {
<br>&nbsp;&nbsp;&nbsp;&nbsp; out.flush();
<br>&nbsp;&nbsp;&nbsp;&nbsp; out.close();
<br>&nbsp;&nbsp; }
<p>} // end class ZipCompressor
<p>============================================================================
<br>============
<br>The main() :
<p>import java.io.*;
<p>public class Test {
<p>&nbsp; public static void main(String[] args) {
<p>&nbsp;&nbsp;&nbsp; File[] files = {
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
new File("d:/java/tests/tests.jpr"),
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
new File("D:/JBuilder4/jdk1.3/jre/lib/rt.jar"),
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
};
<p>&nbsp;&nbsp;&nbsp; long start = System.currentTimeMillis();
<p>&nbsp;&nbsp;&nbsp; try {
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ZipCompressor compressor = new ZipCompressor(new
<br>FileOutputStream("d:/test.zip"));
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compressor.compressFiles(files);
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compressor.write();
<br>&nbsp;&nbsp;&nbsp; } catch(Exception x) { x.printStackTrace(); }
<p>&nbsp;&nbsp;&nbsp; long stop = System.currentTimeMillis();
<p>&nbsp;&nbsp;&nbsp; System.out.println("Total seconds: " + (stop - start)
/ 1000);
<br>&nbsp; }
<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-mail
References: <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