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
Get Java™ Phrasebook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.