--------------------------------------------------------- Create - TopicsExpress



          

--------------------------------------------------------- Create Zip file with ZipOutputStream. --------------------------------------------------------- Hello everyone, Today I would like to share about how to create a zip file from multiple Files with ZipOutputStream. ZipOutputStream implements an output stream filter for writing files in the ZIP file format. To create a zip file from multiple Files with ZipOutputStream We should: 1. Create a FileOutputStream to write to the file with the specified name, that is the zipFile. 2. Create a new ZipOutputStream from the FileOutputStream, that is an output stream filter for writing files in the ZIP file format. 3. For each one of the Files create a new File instance by the given pathname of the file. 4. Create a FileInputStream by opening a connection to the file. 5. Create a new ZipEntry with the name of the File and begin writing it to the ZipOutputStream. 6. Read up to 1024 bytes of data from the file into an array of bytes, using the read(byte[] b) API method of FileInputStream and write the data to the current ZipEntry data, using write(byte[] b, int off, int len) method of ZipOutputStream. 7. Always remember to close the ZipEntry, the ZipOutputStream and the FileInputStream, using closeEntry() and close() API methods   respectively. Let’s take a look at the code snippet that follows: ------------------------------------------------------------------------------ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * How to compress file into Zip File. * * @author AcroMyanmar09 * */ public class CompressFileInZip {  /**   * Main method to execute the program.   * @param args   */  public static void main(String[] args) {   String zipFile = zipFile.zip;   String[] srcFiles = { file1.txt, file2.txt, file3.txt };   try { // create byte buffer    byte[] buffer = new byte[1024];    FileOutputStream fileOutPutStream = new FileOutputStream(zipFile);    ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutPutStream);    for (int index = 0; index < srcFiles.length; index++) {     File srcFile = new File(srcFiles[index]);     FileInputStream fis = new FileInputStream(srcFile);     // begin writing a new Zip entry,     // positions the stream to the start of the entry data     zipOutputStream.putNextEntry(new ZipEntry(srcFile.getName()));     int length;     while ((length = fis.read(buffer)) > 0) {      zipOutputStream.write(buffer, 0, length);     }     zipOutputStream.closeEntry();     // close the InputStream     fis.close();    }        // close the ZipOutputSream    zipOutputStream.close();   } catch (IOException ioe) {    System.out.println(Error creating zip file: + ioe);   }  } } ------------------------------------------------------------------------------ Hope you enjoy it.
Posted on: Thu, 18 Dec 2014 11:01:24 +0000

Trending Topics



Recently Viewed Topics




© 2015