1 package com.opensymphony.webwork.portlet.util; 2 3 import java.io.File ; 4 import java.io.FileInputStream ; 5 import java.io.FileOutputStream ; 6 import java.io.IOException ; 7 import java.util.zip.ZipEntry ; 8 import java.util.zip.ZipOutputStream ; 9 10 public class FolderArchiver { 11 private static final int BUFFER_SIZE = 10240; 12 13 private File folderToArchive; 14 15 private File archiveFile; 16 17 public FolderArchiver(File folderToArchive, File archiveFile) { 18 this.folderToArchive = folderToArchive; 19 this.archiveFile = archiveFile; 20 } 21 22 public void doArchive() throws Exception { 23 FileOutputStream stream = new FileOutputStream (archiveFile); 24 ZipOutputStream output = new ZipOutputStream (stream); 25 compressFile(folderToArchive, output); 26 output.close(); 27 stream.close(); 28 } 29 30 private void compressFile(File file, ZipOutputStream output) throws IOException { 31 if (file == null || !file.exists()) 32 return; 33 if (file.isFile() && !file.equals(archiveFile)) { 34 byte buffer[] = new byte[10240]; 35 String path = file.getPath().substring(folderToArchive.getPath().length()); 36 path = path.replaceAll("\\\\", "/"); 37 if (path.length() > 0 && path.charAt(0) == '/') 38 path = path.substring(1); 39 ZipEntry entry = new ZipEntry (path); 40 entry.setTime(file.lastModified()); 41 output.putNextEntry(entry); 42 FileInputStream in = new FileInputStream (file); 43 int data; 44 while ((data = in.read(buffer, 0, buffer.length)) > 0) output.write(buffer, 0, data); 45 in.close(); 46 } else if (file.isDirectory()) { 47 File files[] = file.listFiles(); 48 for (int i = 0; i < files.length; i++) 49 compressFile(files[i], output); 50 51 } 52 } 53 54 } 55 56 | Popular Tags |