1 package de.jwi.zip; 2 3 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.zip.ZipEntry ; 32 import java.util.zip.ZipOutputStream ; 33 34 import org.apache.commons.io.FileUtils; 35 import org.apache.commons.io.filefilter.IOFileFilter; 36 37 42 public class Zipper 43 { 44 45 private static final int BUFSIZE = 1024; 46 47 51 52 public void zip(ZipOutputStream out, File f, File base) 53 throws IOException 54 { 55 String name = f.getPath().replace('\\', '/'); 56 57 if (base != null) 58 { 59 String basename = base.getPath().replace('\\', '/'); 60 61 if (name.startsWith(basename)) 62 { 63 name = name.substring(basename.length()); 64 } 65 } 66 67 if (name.startsWith("/")) 68 { 69 name = name.substring(1); 70 } 71 72 ZipEntry entry = new ZipEntry (name); 73 entry.setTime(f.lastModified()); 74 75 out.putNextEntry(entry); 76 77 FileInputStream is = new FileInputStream (f); 78 79 byte[] buf = new byte[BUFSIZE]; 80 81 try 82 { 83 int l; 84 while ((l = is.read(buf)) > -1) 85 { 86 out.write(buf, 0, l); 87 } 88 } 89 finally 90 { 91 is.close(); 92 } 93 out.closeEntry(); 94 95 } 96 97 public void zip(ZipOutputStream out, Collection c, File vpath) 98 throws IOException 99 { 100 Iterator it = c.iterator(); 101 while (it.hasNext()) 102 { 103 File f = (File ) it.next(); 104 zip(out, f, vpath); 105 } 106 } 107 108 public static void main(String [] args) throws Exception 109 { 110 ZipOutputStream z = new ZipOutputStream (new FileOutputStream ( 111 "d:/temp/myfirst.zip")); 112 113 IOFileFilter filter = new IOFileFilter() 114 { 115 116 public boolean accept(java.io.File file) 117 { 118 return true; 119 } 120 121 public boolean accept(java.io.File dir, java.lang.String name) 122 { 123 return true; 124 } 125 }; 126 127 Collection c = FileUtils.listFiles(new File ( 128 "/java/javadocs/j2sdk-1.4.1/docs/tooldocs"), filter, filter); 129 130 new Zipper().zip(z, c, new File ("/java/javadocs/j2sdk-1.4.1")); 131 132 z.close(); 133 134 } 135 } | Popular Tags |