1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
/* Create Zip File From Directory recursively using ZipOutputStream Example This Java example shows how create zip file from directory recursively using Java ZipOutputStream class. This program also shows how to add directory and sub-directories to zip file. */ 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.ZipException; import java.util.zip.ZipOutputStream; public class CreateZipDirectoryRecursively { public static void main(String args[]) { String sourceDir = "C:/examples"; String zipFile = "C:/FileIO/zipdemo.zip"; try { //create object of FileOutputStream FileOutputStream fout = new FileOutputStream(zipFile); //create object of ZipOutputStream from FileOutputStream ZipOutputStream zout = new ZipOutputStream(fout); //create File object from source directory File fileSource = new File(sourceDir); addDirectory(zout, fileSource); //close the ZipOutputStream zout.close(); System.out.println("Zip file has been created!"); } catch(IOException ioe) { System.out.println("IOException :" + ioe); } } private static void addDirectory(ZipOutputStream zout, File fileSource) { //get sub-folder/files list File[] files = fileSource.listFiles(); System.out.println("Adding directory " + fileSource.getName()); for(int i=0; i < files.length; i++) { //if the file is directory, call the function recursively if(files[i].isDirectory()) { addDirectory(zout, files[i]); continue; } /* * we are here means, its file and not directory, so * add it to the zip file */ try { System.out.println("Adding file " + files[i].getName()); //create byte buffer byte[] buffer = new byte[1024]; //create object of FileInputStream FileInputStream fin = new FileInputStream(files[i]); zout.putNextEntry(new ZipEntry(files[i].getName())); /* * After creating entry in the zip file, actually * write the file. */ int length; while((length = fin.read(buffer)) > 0) { zout.write(buffer, 0, length); } /* * After writing the file to ZipOutputStream, use * * void closeEntry() method of ZipOutputStream class to * close the current entry and position the stream to * write the next entry. */ zout.closeEntry(); //close the InputStream fin.close(); } catch(IOException ioe) { System.out.println("IOException :" + ioe); } } } } /* Output of this program would be Adding directory examples Adding directory FileIO Adding directory WebFiles Adding directory css Adding file datagrid.css Adding file graph.css Adding directory jsps Adding file Main.jtpl Adding file Masthead.jspf Adding file Welcome.jsp Adding file WebFiles.zip Adding file zipdemo.zip Adding file nonav.log Adding file ospreg.exe Adding file servers.ini Adding file setupisam.log Adding file sourceFile1.doc Adding file sourceFile2.doc Zip file has been created! */ |
Add Comment