Article View: borland.public.jbuilder.java.language
Article #7379Re: ZipOutputStream question - please help!!!
From: NG
Date: Mon, 25 Jun 2001 13:10
Date: Mon, 25 Jun 2001 13:10
305 lines
12025 bytes
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 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" <<A=20
href=3D"mailto:vpatryshev@borland.com">vpatryshev@borland.com</A>> =
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> BufferedReader in =3D new=20
BufferedReader(new FileReader(files[i]));</TT><TT></TT>=20
<P><TT> int c;</TT><TT></TT>=20
<P><TT> while((c =3D in.read()) =
!=3D -1)</TT>=20
<BR><TT> =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> * This class compresses files with the zip format =
and=20
writes the output to <BR>a specified OutputStream. <BR> */=20
<P>public class ZipCompressor {=20
<P> private ZipOutputStream out;=20
<P> /** <BR> * The constructor accepts an =
OutputStream=20
object to which the data will <BR>be written to. <BR> *=20
<BR> * @param outputStream - the outputstream to which =
the=20
compressed files <BR>will be written. <BR> */=20
<P> public ZipCompressor(OutputStream outputStream) {=20
<BR> out =3D new ZipOutputStream(new=20
BufferedOutputStream(outputStream)); <BR> }=20
<P> /** <BR> * The method accepts a File array =
containing=20
the files to compress. <BR> * <BR> * @param =
files -=20
a File array containing the desired files to compress. =
<BR> *=20
<BR> * @throws FileNotFoundException - if a file in the =
array is=20
not found. <BR> * @throws IOException - if an =
IOException=20
occurs. <BR> */=20
<P> public void compressFiles(File[] files) throws=20
FileNotFoundException, <BR>IOException { =
<BR> =20
for(int i =3D 0; i < files.length; i++) =
<BR> {=20
<BR> String fileName =3D=20
files[i].getName();=20
<P> out.putNextEntry(new=20
ZipEntry(fileName)); <BR> =
BufferedReader=20
in =3D new BufferedReader(new FileReader(files[i]));=20
<P> int c;=20
<P> while((c =3D in.read()) !=3D =
-1)=20
<BR> out.write(c);=20
<P> in.close();=20
<P> } // end for loop <BR> }=20
<P> /** <BR> * Writes the compressed =
data to=20
the outputstream (which was specified in <BR>the constructor).=20
<BR> * <BR> * @throws =
IOException - if=20
the closing the stream fails. <BR> */=20
<P> public void write() throws IOException {=20
<BR> out.flush(); =
<BR> =20
out.close(); <BR> }=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> public static void main(String[] args) {=20
<P> File[] files =3D {=20
=
<BR> &nb=
sp; =20
new File("d:/java/tests/tests.jpr"),=20
=
<BR> &nb=
sp; =20
new File("D:/JBuilder4/jdk1.3/jre/lib/rt.jar"),=20
=
<BR> &nb=
sp; =20
};=20
<P> long start =3D System.currentTimeMillis();=20
<P> try { <BR> =
ZipCompressor=20
compressor =3D new ZipCompressor(new =
<BR>FileOutputStream("d:/test.zip"));=20
<BR> compressor.compressFiles(files);=20
<BR> compressor.write();=20
<BR> } catch(Exception x) { x.printStackTrace(); } =
<P> long stop =3D System.currentTimeMillis();=20
<P> System.out.println("Total seconds: " + (stop - =
start)=20
/ 1000); <BR> }=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!dnewsReferences:
<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