1 5 package com.tc.util; 6 7 import org.apache.commons.io.CopyUtils; 8 9 import java.io.BufferedInputStream ; 10 import java.io.BufferedOutputStream ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.util.HashSet ; 17 import java.util.zip.CRC32 ; 18 import java.util.zip.ZipEntry ; 19 import java.util.zip.ZipInputStream ; 20 import java.util.zip.ZipOutputStream ; 21 22 25 public class ZipBuilder implements ArchiveBuilder { 26 27 private final CRC32 crc32 = new CRC32 (); 28 private final ZipOutputStream zout; 29 private final HashSet dirSet = new HashSet (); 30 private final HashSet entrySet = new HashSet (); 31 32 public ZipBuilder(File archiveFile, boolean useCompression) throws IOException { 33 zout = getArchiveOutputStream(archiveFile); 34 if (useCompression) { 35 zout.setMethod(ZipEntry.DEFLATED); 36 zout.setLevel(9); 37 } else { 38 zout.setMethod(ZipEntry.STORED); 39 zout.setLevel(0); 40 } 41 } 42 43 public final void putTraverseDirectory(File dir, String dirName) throws IOException { 44 if (!dir.isDirectory()) throw new IOException ("Unexpected Exception: " + dir + "\nis not a directory"); 45 putDirEntry(dirName); 46 String [] files = dir.list(); 47 for (int i = 0; i < files.length; i++) { 48 File file = new File (dir + File.separator + files[i]); 49 if (file.isDirectory()) { 50 putTraverseDirectory(file, dirName + File.separator + file.getName()); 51 continue; 52 } 53 putEntry(dirName + File.separator + files[i], readFile(file)); 54 } 55 } 56 57 public final void putDirEntry(String file) throws IOException { 58 if (dirSet.contains(file)) return; 59 dirSet.add(file); 60 String dirEntry = archivePath(file) + "/"; 61 ZipEntry entry = createEntry(dirEntry); 62 entry.setSize(0); 63 entry.setCrc(0); 64 zout.putNextEntry(entry); 65 System.out.println(dirEntry); 66 } 67 68 public final void putEntry(String file, byte[] bytes) throws IOException { 69 if (entrySet.contains(file.toString())) return; 70 entrySet.add(file.toString()); 71 String fileEntry = archivePath(file); 72 ZipEntry entry = createEntry(fileEntry); 73 entry.setSize(bytes.length); 74 entry.setCrc(getCrc32(bytes)); 75 zout.putNextEntry(entry); 76 zout.write(bytes, 0, bytes.length); 77 System.out.println(fileEntry); 78 } 79 80 public final void finish() throws IOException { 81 zout.close(); 82 } 83 84 public final byte[] readFile(File file) throws IOException { 85 BufferedInputStream in = new BufferedInputStream (new FileInputStream (file)); 86 byte[] bytes = new byte[in.available()]; 87 in.read(bytes); 88 in.close(); 89 return bytes; 90 } 91 92 protected ZipEntry createEntry(String name) { 93 return new ZipEntry (name); 94 } 95 96 protected ZipOutputStream getArchiveOutputStream(File archiveFile) throws IOException { 97 if (zout != null) throw new IllegalStateException ("ArchiveOutputStream has already been instantiated."); 98 return new ZipOutputStream (new BufferedOutputStream (new FileOutputStream (archiveFile))); 99 } 100 101 private long getCrc32(byte[] bytes) { 102 crc32.update(bytes); 103 long checksum = crc32.getValue(); 104 crc32.reset(); 105 return checksum; 106 } 107 108 private String archivePath(String file) { 109 if (File.separator.equals("/")) return file; 110 return file.replaceAll("\\\\", "/"); 111 } 112 113 public static void unzip(InputStream archive, File destDir) throws IOException { 114 ZipInputStream zis = new ZipInputStream (archive); 115 ZipEntry entry; 116 while ((entry = zis.getNextEntry()) != null) { 117 File file = new File (destDir, entry.getName()); 118 if (!entry.getName().endsWith("/")) { 119 CopyUtils.copy(zis, new FileOutputStream (file)); 120 } else { 121 file.mkdirs(); 122 } 123 zis.closeEntry(); 124 } 125 } 126 } 127 | Popular Tags |