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 140 |
/* Extract Zip File With Subdirectories Using Command Line Argument Example. This Java example shows how to extract a zip file and create required sub-directories using Java ZipInputStream class. */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; public class ExtractFileSubDirectories { public static void main(String args[]) { /* * This program expects one command line argument * * 1. Path of zip file */ //fetch command line argument String strZipFile = args[0]; if(strZipFile == null || strZipFile.equals("")) { System.out.println("Invalid source file"); System.exit(0); } //call method to unzip/extract files from zip file unzip(strZipFile); System.out.println("Zip file extracted!"); } private static void unzip(String strZipFile) { try { /* * STEP 1 : Create directory with the name of the zip file * * For e.g. if we are going to extract c:/demo.zip create c:/demo * directory where we can extract all the zip entries * */ File fSourceZip = new File(strZipFile); String zipPath = strZipFile.substring(0, strZipFile.length()-4); File temp = new File(zipPath); temp.mkdir(); System.out.println(zipPath + " created"); /* * STEP 2 : Extract entries while creating required * sub-directories * */ ZipFile zipFile = new ZipFile(fSourceZip); Enumeration e = zipFile.entries(); while(e.hasMoreElements()) { ZipEntry entry = (ZipEntry)e.nextElement(); File destinationFilePath = new File(zipPath,entry.getName()); //create directories if required. destinationFilePath.getParentFile().mkdirs(); //if the entry is directory, leave it. Otherwise extract it. if(entry.isDirectory()) { continue; } else { System.out.println("Extracting " + destinationFilePath); /* * Get the InputStream for current entry * of the zip file using * * InputStream getInputStream(Entry entry) method. */ BufferedInputStream bis = new BufferedInputStream(zipFile .getInputStream(entry)); int b; byte buffer[] = new byte[1024]; /* * read the current entry from the zip file, extract it * and write the extracted file. */ FileOutputStream fos = new FileOutputStream(destinationFilePath); BufferedOutputStream bos = new BufferedOutputStream(fos, 1024); while ((b = bis.read(buffer, 0, 1024)) != -1) { bos.write(buffer, 0, b); } //flush the output stream and close it. bos.flush(); bos.close(); //close the input stream. bis.close(); } } } catch(IOException ioe) { System.out.println("IOError :" + ioe); } } } /* Sample usage of this prgram java ExtractFileSubDirectories c:/sampleDoc.zip Output of this program would be Zip file extracted! This program will extract file from C:/sampleDoc.zip to c:/sampleDoc directory with all required sub-directories. */ |
Add Comment