1 21 package proguard.io; 22 23 import java.io.*; 24 import java.util.jar.*; 25 import java.util.*; 26 import java.util.zip.*; 27 28 29 35 public class JarWriter implements DataEntryWriter, Finisher 36 { 37 private DataEntryWriter dataEntryWriter; 38 private Manifest manifest; 39 private String comment; 40 41 private OutputStream currentParentOutputStream; 42 private ZipOutputStream currentJarOutputStream; 43 private Finisher currentFinisher; 44 private String currentEntryName; 45 46 private Set jarEntryNames = new HashSet(); 48 49 50 53 public JarWriter(DataEntryWriter dataEntryWriter) 54 { 55 this(dataEntryWriter, null, null); 56 } 57 58 59 62 public JarWriter(DataEntryWriter dataEntryWriter, 63 Manifest manifest, 64 String comment) 65 { 66 this.dataEntryWriter = dataEntryWriter; 67 this.manifest = manifest; 68 this.comment = comment; 69 } 70 71 72 74 public OutputStream getOutputStream(DataEntry dataEntry) throws IOException 75 { 76 return getOutputStream(dataEntry, null); 77 } 78 79 80 public OutputStream getOutputStream(DataEntry dataEntry, 81 Finisher finisher) throws IOException 82 { 83 OutputStream parentOutputStream = 86 dataEntryWriter.getOutputStream(dataEntry.getParent(), this); 87 88 if (parentOutputStream == null) 90 { 91 return null; 92 } 93 94 if (currentParentOutputStream == null) 96 { 97 currentParentOutputStream = parentOutputStream; 98 99 currentJarOutputStream = manifest != null ? 101 new JarOutputStream(parentOutputStream, manifest) : 102 new ZipOutputStream(parentOutputStream); 103 104 if (comment != null) 106 { 107 currentJarOutputStream.setComment(comment); 108 } 109 } 110 111 String name = dataEntry.getName(); 113 114 if (!name.equals(currentEntryName)) 116 { 117 closeEntry(); 119 120 if (!jarEntryNames.add(name)) 124 { 125 throw new IOException("Duplicate zip entry ["+dataEntry+"]"); 126 } 127 128 currentJarOutputStream.putNextEntry(new ZipEntry(name)); 130 131 currentFinisher = finisher; 132 currentEntryName = name; 133 } 134 135 return currentJarOutputStream; 136 } 137 138 139 public void finish() throws IOException 140 { 141 if (currentJarOutputStream != null) 143 { 144 closeEntry(); 146 147 currentJarOutputStream.finish(); 149 currentJarOutputStream = null; 150 currentParentOutputStream = null; 151 jarEntryNames.clear(); 152 } 153 } 154 155 156 public void close() throws IOException 157 { 158 dataEntryWriter.close(); 160 } 161 162 163 165 168 private void closeEntry() throws IOException 169 { 170 if (currentEntryName != null) 171 { 172 if (currentFinisher != null) 174 { 175 currentFinisher.finish(); 176 currentFinisher = null; 177 } 178 179 currentJarOutputStream.closeEntry(); 180 currentEntryName = null; 181 } 182 } 183 } 184 | Popular Tags |