O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a Zip Archive

// writing a zip file
											ZipOutputStream out =
											new ZipOutputStream(
											new FileoutputStream(zipFileName));
											FileInputStream in = new FileInputStream(fileToZip1);
											out.putNextEntry(new ZipEntry(fileToZip1));
											int len;
											byte[] buf = new byte[1024];
											while ((len = in.read(buf)) > 0) {
											out.write(buf,0,len);
											}
											out.closeEntry();
											in.close();
											out.close();

In the previous phrase, we showed how you can read from a Zip file. In this phrase, we create a Zip file. To do this, we first construct a ZipOutputStream by passing to its constructor a FileOutputStream object pointing to the file we want to create as our Zip file. We then create a FileInputStream for a file that we want to add to our Zip file. We use the putNextEntry() method of the

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required