🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: ZipOutputStream question - please help!!!

#7379
From: NG
Date: Mon, 25 Jun 2001 13:10
305 lines
12025 bytes
C'est un message de format MIME en plusieurs parties.

------=_NextPart_000_00A0_01C0FD89.053A7D40
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

There is a much faster method there :
http://www.j-nine.com/pubs/javazip/Java_Zip.html
  "Vladimir Patryshev" <vpatryshev@borland.com> a =E9crit dans le =
message news: 3AA99791.B186129B@borland.com...
  Hi,=20
  You could probably make your code more efficient if you replace this:=20

         BufferedReader in =3D new BufferedReader(new =
FileReader(files[i]));=20

         int c;=20

         while((c =3D in.read()) !=3D -1)=20
           out.write(c);=20

  with something that does it in blocks.=20

  Thanks,=20
  Vlad=20

  Yonatan Taub wrote:=20

    Hello!=20
    I've written a simple object which accepts a File[] array and =
compresses it=20
    with a ZipOutputStream object to a given OutputStream.=20
    It works fine. The only problem is when I try to compress about =
12MB, it=20
    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=20
    buffering?=20

    The compressor object:=20

    import java.util.zip.*;=20
    import java.io.*;=20

    /**=20
     * This class compresses files with the zip format and writes the =
output to=20
    a specified OutputStream.=20
     */=20

    public class ZipCompressor {=20

      private ZipOutputStream out;=20

      /**=20
       * The constructor accepts an OutputStream object to which the =
data will=20
    be written to.=20
       *=20
       * @param outputStream - the outputstream to which the compressed =
files=20
    will be written.=20
       */=20

      public ZipCompressor(OutputStream outputStream) {=20
        out =3D new ZipOutputStream(new =
BufferedOutputStream(outputStream));=20
      }=20

      /**=20
       * The method accepts a File array containing the files to =
compress.=20
       *=20
       * @param files - a File array containing the desired files to =
compress.=20
       *=20
       * @throws FileNotFoundException - if a file in the array is not =
found.=20
       * @throws IOException - if an IOException occurs.=20
       */=20

       public void compressFiles(File[] files) throws =
FileNotFoundException,=20
    IOException {=20
         for(int i =3D 0; i < files.length; i++)=20
         {=20
           String fileName =3D files[i].getName();=20

           out.putNextEntry(new ZipEntry(fileName));=20
           BufferedReader in =3D new BufferedReader(new =
FileReader(files[i]));=20

           int c;=20

           while((c =3D in.read()) !=3D -1)=20
             out.write(c);=20

           in.close();=20

         } // end for loop=20
       }=20

       /**=20
        * Writes the compressed data to the outputstream (which was =
specified in=20
    the constructor).=20
        *=20
        * @throws IOException - if the closing the stream fails.=20
        */=20

       public void write() throws IOException {=20
         out.flush();=20
         out.close();=20
       }=20

    } // end class ZipCompressor=20

    =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=20
    =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=20
    The main() :=20

    import java.io.*;=20

    public class Test {=20

      public static void main(String[] args) {=20

        File[] files =3D {=20
                         new File("d:/java/tests/tests.jpr"),=20
                         new File("D:/JBuilder4/jdk1.3/jre/lib/rt.jar"), =

                       };=20

        long start =3D System.currentTimeMillis();=20

        try {=20
          ZipCompressor compressor =3D new ZipCompressor(new=20
    FileOutputStream("d:/test.zip"));=20
          compressor.compressFiles(files);=20
          compressor.write();=20
        } catch(Exception x) { x.printStackTrace(); }=20

        long stop =3D System.currentTimeMillis();=20

        System.out.println("Total seconds: " + (stop - start) / 1000);=20
      }=20

    } // end class Test=20

    Thanks!!!!


------=_NextPart_000_00A0_01C0FD89.053A7D40
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Diso-8859-1" =
http-equiv=3DContent-Type>
<META content=3D"MSHTML 5.00.3315.2870" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DArial size=3D2>There is a&nbsp;much faster method =
there=20
:</FONT></DIV>
<DIV><FONT face=3DArial size=3D2><A=20
href=3D"http://www.j-nine.com/pubs/javazip/Java_Zip.html">http://www.j-ni=
ne.com/pubs/javazip/Java_Zip.html</A></FONT></DIV>
<BLOCKQUOTE=20
style=3D"BORDER-LEFT: #000000 2px solid; MARGIN-LEFT: 5px; MARGIN-RIGHT: =
0px; PADDING-LEFT: 5px; PADDING-RIGHT: 0px">
  <DIV>"Vladimir Patryshev" &lt;<A=20
  href=3D"mailto:vpatryshev@borland.com">vpatryshev@borland.com</A>&gt; =
a =E9crit=20
  dans le message news: <A=20
  =
href=3D"mailto:3AA99791.B186129B@borland.com">3AA99791.B186129B@borland.c=
om</A>...</DIV>Hi,=20

  <P>You could probably make your code more efficient if you replace =
this:=20
  <P><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; BufferedReader in =3D new=20
  BufferedReader(new FileReader(files[i]));</TT><TT></TT>=20
  <P><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int c;</TT><TT></TT>=20
  <P><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while((c =3D in.read()) =
!=3D -1)</TT>=20
  <BR><TT>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
  out.write(c);</TT><TT></TT>=20
  <P>with something that does it in blocks.=20
  <P>Thanks, <BR>Vlad=20
  <P>Yonatan Taub wrote:=20
  <BLOCKQUOTE TYPE=3D"CITE">Hello!=20
    <P>I've written a simple object which accepts a File[] array and =
compresses=20
    it <BR>with a ZipOutputStream object to a given OutputStream. <BR>It =
works=20
    fine. The only problem is when I try to compress about 12MB, it =
<BR>take=20
    about 50 seconds to complete while WinZip does it in 6 seconds.=20
    <P>Does my code has a logical flaw? Do I need to do something with =
the=20
    <BR>buffering?=20
    <P>The compressor object:=20
    <P>import java.util.zip.*; <BR>import java.io.*;=20
    <P>/** <BR>&nbsp;* This class compresses files with the zip format =
and=20
    writes the output to <BR>a specified OutputStream. <BR>&nbsp;*/=20
    <P>public class ZipCompressor {=20
    <P>&nbsp; private ZipOutputStream out;=20
    <P>&nbsp; /** <BR>&nbsp;&nbsp; * The constructor accepts an =
OutputStream=20
    object to which the data will <BR>be written to. <BR>&nbsp;&nbsp; *=20
    <BR>&nbsp;&nbsp; * @param outputStream - the outputstream to which =
the=20
    compressed files <BR>will be written. <BR>&nbsp;&nbsp; */=20
    <P>&nbsp; public ZipCompressor(OutputStream outputStream) {=20
    <BR>&nbsp;&nbsp;&nbsp; out =3D new ZipOutputStream(new=20
    BufferedOutputStream(outputStream)); <BR>&nbsp; }=20
    <P>&nbsp; /** <BR>&nbsp;&nbsp; * The method accepts a File array =
containing=20
    the files to compress. <BR>&nbsp;&nbsp; * <BR>&nbsp;&nbsp; * @param =
files -=20
    a File array containing the desired files to compress. =
<BR>&nbsp;&nbsp; *=20
    <BR>&nbsp;&nbsp; * @throws FileNotFoundException - if a file in the =
array is=20
    not found. <BR>&nbsp;&nbsp; * @throws IOException - if an =
IOException=20
    occurs. <BR>&nbsp;&nbsp; */=20
    <P>&nbsp;&nbsp; public void compressFiles(File[] files) throws=20
    FileNotFoundException, <BR>IOException { =
<BR>&nbsp;&nbsp;&nbsp;&nbsp;=20
    for(int i =3D 0; i &lt; files.length; i++) =
<BR>&nbsp;&nbsp;&nbsp;&nbsp; {=20
    <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String fileName =3D=20
    files[i].getName();=20
    <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.putNextEntry(new=20
    ZipEntry(fileName)); <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
BufferedReader=20
    in =3D new BufferedReader(new FileReader(files[i]));=20
    <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int c;=20
    <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while((c =3D in.read()) !=3D =
-1)=20
    <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; out.write(c);=20
    <P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; in.close();=20
    <P>&nbsp;&nbsp;&nbsp;&nbsp; } // end for loop <BR>&nbsp;&nbsp; }=20
    <P>&nbsp;&nbsp; /** <BR>&nbsp;&nbsp;&nbsp; * Writes the compressed =
data to=20
    the outputstream (which was specified in <BR>the constructor).=20
    <BR>&nbsp;&nbsp;&nbsp; * <BR>&nbsp;&nbsp;&nbsp; * @throws =
IOException - if=20
    the closing the stream fails. <BR>&nbsp;&nbsp;&nbsp; */=20
    <P>&nbsp;&nbsp; public void write() throws IOException {=20
    <BR>&nbsp;&nbsp;&nbsp;&nbsp; out.flush(); =
<BR>&nbsp;&nbsp;&nbsp;&nbsp;=20
    out.close(); <BR>&nbsp;&nbsp; }=20
    <P>} // end class ZipCompressor=20
    =
<P>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=20
    <BR>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D <BR>The main() :=20
    <P>import java.io.*;=20
    <P>public class Test {=20
    <P>&nbsp; public static void main(String[] args) {=20
    <P>&nbsp;&nbsp;&nbsp; File[] files =3D {=20
    =
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
    new File("d:/java/tests/tests.jpr"),=20
    =
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
    new File("D:/JBuilder4/jdk1.3/jre/lib/rt.jar"),=20
    =
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
    };=20
    <P>&nbsp;&nbsp;&nbsp; long start =3D System.currentTimeMillis();=20
    <P>&nbsp;&nbsp;&nbsp; try { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
ZipCompressor=20
    compressor =3D new ZipCompressor(new =
<BR>FileOutputStream("d:/test.zip"));=20
    <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compressor.compressFiles(files);=20
    <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; compressor.write();=20
    <BR>&nbsp;&nbsp;&nbsp; } catch(Exception x) { x.printStackTrace(); } =

    <P>&nbsp;&nbsp;&nbsp; long stop =3D System.currentTimeMillis();=20
    <P>&nbsp;&nbsp;&nbsp; System.out.println("Total seconds: " + (stop - =
start)=20
    / 1000); <BR>&nbsp; }=20
    <P>} // end class Test=20
    <P>Thanks!!!!</P></BLOCKQUOTE></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_00A0_01C0FD89.053A7D40--
Technical Details
Message-ID: <3b37396b_2@dnews>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews
References: <3aa80218_1@dnews> <3AA99791.B186129B@borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: f74b3,f1b1ab5b0816f6b8
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-06-25 06:30:38 PST
From: "NG" <stagiaire@gitis.fr>
Newsgroups: borland.public.jbuilder.java.language
References: <3aa80218_1@dnews> <3AA99791.B186129B@borland.com>
Subject: Re: ZipOutputStream question - please help!!!
Date: Mon, 25 Jun 2001 15:10:50 +0200
Lines: 306
MIME-Version: 1.0
Content-Type: multipart/alternative;
 boundary="----=_NextPart_000_00A0_01C0FD89.053A7D40"
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
NNTP-Posting-Host: 194.206.187.214
Message-ID: <3b37396b_2@dnews>
X-Trace: dnews 993474923 194.206.187.214 (25 Jun 2001 06:15:23 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!sn-xit-02!supernews.com!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!dnews