1 26 27 package org.objectweb.openccm.packaging; 28 29 import java.io.*; 30 import java.util.zip.*; 31 32 38 public class ZipFactory 39 { 40 46 47 private java.util.List files_to_zip_; 48 49 50 private String zipname_; 51 52 58 61 public 62 ZipFactory() 63 { 64 files_to_zip_ = new java.util.LinkedList (); 66 67 } 68 69 75 81 private void 82 compressFile(File inputfile, ZipOutputStream zos) 83 throws Exception 84 { 85 byte[] buff = new byte[2048]; 86 FileInputStream fis = new FileInputStream(inputfile); 87 88 int ret = 0; 89 90 zos.putNextEntry(new ZipEntry(inputfile.getPath())); 91 while (ret!=-1) { 92 ret = fis.read(buff, 0, 2048); 93 if (ret!=-1) { 94 zos.write(buff, 0, ret); 95 } 96 } 97 98 zos.closeEntry(); 99 fis.close(); 100 } 101 102 108 113 public void 114 addFileToZip(String filename) 115 { 116 if (! files_to_zip_.contains(filename)) 117 { 118 files_to_zip_.add(filename); 119 } 120 } 121 122 127 public void 128 setZipName(String name) 129 { 130 this.zipname_ = name; 131 } 132 133 137 public void 138 buildZip() 139 throws Exception 140 { 141 String [] files 143 = (String []) files_to_zip_.toArray(new String [0]); 144 145 FileOutputStream fos = new FileOutputStream(zipname_); 147 ZipOutputStream zos = new ZipOutputStream(fos); 148 149 for (int i=0;i<files.length;i++) 151 { 152 153 java.io.File file_to_add 155 = new java.io.File ( 156 files[i] 157 ); 158 159 if ( file_to_add.exists() ) 160 { 161 System.err.println("Adding file: "+file_to_add.getPath()+" to zip"); 162 compressFile( 163 file_to_add, 164 zos 165 ); 166 } else { 167 System.err.println( 168 files[i] 169 +" can not be found -> skipping.." 170 ); 171 } 172 } 173 174 zos.close(); 175 } 176 } 177 178 | Popular Tags |