1 22 package org.objectweb.petals.tools.jbiplugin.util; 23 24 import java.io.DataInputStream ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.util.zip.ZipEntry ; 29 import java.util.zip.ZipOutputStream ; 30 31 39 public final class ZipUtil { 40 41 44 private ZipUtil() { 45 super(); 46 } 47 48 60 public static void zipDirectory(File archive, File directory, 61 String entryDirectoryName) throws Exception { 62 if (directory.exists()) { 63 ZipOutputStream zipOutputStream = new ZipOutputStream ( 64 new FileOutputStream (archive, true)); 65 recurseDirectory(zipOutputStream, directory, entryDirectoryName); 66 zipOutputStream.flush(); 67 zipOutputStream.close(); 68 } 69 } 70 71 82 private static void recurseDirectory(ZipOutputStream zipOutputStream, 83 File directory, String entryDirectoryName) throws Exception { 84 for (File file : directory.listFiles()) { 85 if (file.isFile()) { 86 ZipEntry zipEntry = new ZipEntry (entryDirectoryName 87 + File.separator + file.getName()); 88 DataInputStream dis = new DataInputStream (new FileInputStream ( 89 file)); 90 byte[] content = new byte[dis.available()]; 91 dis.readFully(content); 92 zipOutputStream.putNextEntry(zipEntry); 93 zipOutputStream.write(content); 94 zipOutputStream.closeEntry(); 95 } else { 96 if (!file.getName().startsWith(".")) { 97 recurseDirectory(zipOutputStream, file, entryDirectoryName 99 + File.separator + file.getName()); 100 } 101 } 102 } 103 } 104 } 105 | Popular Tags |