1 16 package net.sf.jftp.util; 17 18 import net.sf.jftp.*; 19 import net.sf.jftp.net.*; 20 import net.sf.jftp.system.logging.Log; 21 import net.sf.jftp.util.*; 22 23 import java.io.*; 24 25 import java.util.zip.*; 26 27 28 public class ZipFileCreator 29 { 30 private ZipOutputStream z; 31 32 public ZipFileCreator(String [] files, String path, String name) 33 throws Exception 34 { 35 z = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(path + 36 name))); 37 perform(files, path, ""); 38 z.finish(); 39 z.flush(); 40 z.close(); 41 } 42 43 private void perform(String [] files, String path, String offset) 44 { 45 byte[] buf = new byte[4096]; 46 47 for(int i = 0; i < files.length; i++) 48 { 49 try 50 { 51 File f = new File(path + offset + files[i]); 52 BufferedInputStream in = null; 53 54 if(f.exists() && !f.isDirectory()) 55 { 56 in = new BufferedInputStream(new FileInputStream(path + 57 offset + 58 files[i])); 59 } 60 else if(f.exists()) 61 { 62 if(!files[i].endsWith("/")) 63 { 64 files[i] = files[i] + "/"; 65 } 66 67 perform(f.list(), path, offset + files[i]); 68 } 69 70 ZipEntry tmp = new ZipEntry(offset + files[i]); 71 z.putNextEntry(tmp); 72 73 int len = 0; 74 75 while((in != null) && (len != StreamTokenizer.TT_EOF)) 76 { 77 len = in.read(buf); 78 79 if(len == StreamTokenizer.TT_EOF) 80 { 81 break; 82 } 83 84 z.write(buf, 0, len); 85 } 86 87 z.closeEntry(); 88 } 89 catch(Exception ex) 90 { 91 Log.debug("Skipping a file (no permission?)"); 92 } 93 } 94 } 95 } 96 | Popular Tags |