/Users/lyon/j4p/src/bookExamples/ch15Streams/Zipper.java

1    package bookExamples.ch15Streams; 
2     
3    import futils.DirList; 
4    import futils.FileStore; 
5    import futils.Futil; 
6    import gui.In; 
7     
8    import java.io.File; 
9    import java.io.FileOutputStream; 
10   import java.io.IOException; 
11   import java.io.InputStream; 
12   import java.util.Enumeration; 
13   import java.util.Vector; 
14   import java.util.zip.ZipEntry; 
15   import java.util.zip.ZipFile; 
16   import java.util.zip.ZipOutputStream; 
17    
18   import gui.In; 
19    
20   /** 
21    * DocJava, Inc. 
22    * http://www.docjava.com 
23    * Programmer: dlyon 
24    * Date: Sep 22, 2004 
25    * Time: 7:20:01 PM 
26    */ 
27   public class Zipper { 
28       public static void main(String[] args) { 
29           zipDirectory(); 
30           In.message("done"); 
31       } 
32    
33       private static void zipDirectory() { 
34           Futil.setSwing(false); 
35           File outputFile = Futil.getWriteFile("select an output zipFile"); 
36           DirList dl = new DirList(""); 
37           File f[] = dl.getFilesNotDirectories(); 
38           try { 
39               writeZipFiles(outputFile, f); 
40           } catch (IOException e) { 
41               e.printStackTrace(); 
42           } 
43       } 
44    
45       public static void writeZipFiles(File outputFile, File filesToBeWritten[]) 
46               throws IOException { 
47           ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(outputFile)); 
48           zos.setMethod(ZipOutputStream.DEFLATED); 
49           for (int i = 0; i < filesToBeWritten.length; i++) 
50               putEntry(filesToBeWritten[i], zos); 
51           zos.close(); 
52       } 
53    
54       private static void putEntry(File inputFile, ZipOutputStream zos) throws IOException { 
55           zos.putNextEntry(new ZipEntry(inputFile.getAbsolutePath())); 
56           zos.write(Futil.getBytes(inputFile)); 
57           zos.closeEntry(); 
58       } 
59   } 
60    /*   public static FileStore[] readZipFiles(File inputFile) 
61               throws IOException { 
62           ZipFile zf = new ZipFile(inputFile); 
63           Vector fileStoreVector = new Vector(); 
64           Enumeration e = zf.entries(); 
65           while (e.hasMoreElements()) { 
66               ZipEntry ze = (ZipEntry) e.nextElement(); 
67               String name = ze.getName(); 
68               if (ze.isDirectory()) continue; 
69               InputStream is = zf.getInputStream(ze); 
70               int i = is.available(); 
71               byte b[] = new byte[i]; 
72               is.read(b); 
73               FileStore fs = new FileStore(new File(name), b); 
74               fileStoreVector.addElement(fs); 
75           } 
76           zf.close(); 
77           return null;// todo not done yet.... 
78       } 
79   */