1 22 23 package org.meshcms.util; 24 25 import java.io.*; 26 import java.util.zip.*; 27 28 31 public class ZipArchiver extends DirectoryParser { 32 private ZipOutputStream zout; 33 private byte[] buf; 34 35 41 public ZipArchiver(File contents, OutputStream out) { 42 zout = new ZipOutputStream(out); 43 setInitialDir(contents); 44 setRecursive(true); 45 buf = new byte[Utils.BUFFER_SIZE]; 46 } 47 48 protected boolean preProcessDirectory(File file, Path path) { 49 try { 50 ZipEntry ze = new ZipEntry(path + "/"); 51 ze.setTime(file.lastModified()); 52 zout.putNextEntry(ze); 53 zout.closeEntry(); 54 } catch (IOException ex) { 55 ex.printStackTrace(); 56 return false; 57 } 58 59 return true; 60 } 61 62 protected void processFile(File file, Path path) { 63 try { 64 ZipEntry ze = new ZipEntry(path.isRoot() ? file.getName() : path.toString()); 65 ze.setTime(file.lastModified()); 66 ze.setSize(file.length()); 67 zout.putNextEntry(ze); 68 FileInputStream fis = new FileInputStream(file); 69 int len; 70 71 while((len = fis.read(buf)) != -1) { 72 zout.write(buf, 0, len); 73 } 74 75 fis.close(); 76 zout.closeEntry(); 77 } catch (IOException ex) { 78 ex.printStackTrace(); 79 } 80 } 81 82 protected void postProcess() { 83 try { 84 zout.finish(); 85 zout.flush(); 86 } catch (IOException ex) { 87 ex.printStackTrace(); 88 } 89 } 90 } 91 | Popular Tags |