1 6 21 22 package de.schlichtherle.io.archive.zip; 23 24 import de.schlichtherle.io.*; 25 import de.schlichtherle.io.archive.spi.*; 26 import de.schlichtherle.util.zip.*; 27 28 import java.io.*; 29 import java.util.*; 30 import java.util.zip.*; 31 32 41 public class Zip32OutputArchive 42 extends BasicZipOutputStream 43 implements OutputArchive { 44 45 private final Zip32InputArchive source; 46 private OutputArchiveMetaData metaData; 47 48 52 public Zip32OutputArchive( 53 final OutputStream out, 54 final String encoding, 55 final Zip32InputArchive source) 56 throws NullPointerException , 57 UnsupportedEncodingException, 58 IOException { 59 super(out, encoding); 60 super.setLevel(Deflater.BEST_COMPRESSION); 61 62 this.source = source; 63 if (source != null) { 64 super.setComment(source.getComment()); 66 if (source.getPreambleLength() > 0) { 67 final InputStream in = source.getPreambleInputStream(); 68 try { 69 de.schlichtherle.io.File.cat( 70 in, source.offsetsConsiderPreamble() ? this : out); 71 } finally { 72 in.close(); 73 } 74 } 75 } 76 } 77 78 public int getNumArchiveEntries() { 79 return size(); 80 } 81 82 public Enumeration getArchiveEntries() { 83 return entries(); 84 } 85 86 public ArchiveEntry getArchiveEntry(final String name) { 87 return (Zip32Entry) getEntry(name); 88 } 89 90 public OutputStream getOutputStream( 91 final ArchiveEntry entry, 92 final ArchiveEntry srcEntry) 93 throws IOException { 94 if (busy()) 95 throw new OutputArchiveBusyException(entry); 96 97 final Zip32Entry zipEntry = (Zip32Entry) entry; 98 if (srcEntry instanceof Zip32Entry) { 99 final Zip32Entry srcZipEntry = (Zip32Entry) srcEntry; 101 zipEntry.setMethod(srcZipEntry.getMethod()); 102 zipEntry.setCrc(srcZipEntry.getCrc()); 103 zipEntry.setSize(srcZipEntry.getSize()); 104 zipEntry.setCompressedSize(srcZipEntry.getCompressedSize()); 105 } 106 107 return createEntryOutputStream(zipEntry, srcEntry); 108 } 109 110 protected OutputStream createEntryOutputStream( 111 final Zip32Entry entry, 112 final ArchiveEntry srcEntry) 113 throws IOException { 114 return new EntryOutputStream(entry, !(srcEntry instanceof Zip32Entry)); 115 } 116 117 protected class EntryOutputStream extends FilterOutputStream { 118 public EntryOutputStream(Zip32Entry entry, boolean deflate) 119 throws IOException { 120 super(Zip32OutputArchive.this); 121 putNextEntry(entry, deflate); 122 } 123 124 public void write(int b) throws IOException { 125 out.write(b); 126 } 127 128 public void write(byte[] b) throws IOException { 129 out.write(b); 130 } 131 132 public void write(byte[] b, int off, int len) throws IOException { 133 out.write(b, off, len); 134 } 135 136 public void close() throws IOException { 137 assert out == Zip32OutputArchive.this; 138 closeEntry(); 139 } 140 } 142 public void storeDirectory(ArchiveEntry entry) 143 throws IOException { 144 assert entry.isDirectory(); 145 final Zip32Entry ze = (Zip32Entry) entry; 146 ze.setMethod(Zip32Entry.STORED); 147 ze.setCrc(0); 148 ze.setSize(0); 149 putNextEntry(ze); 150 closeEntry(); 151 } 152 153 156 public void finish() throws IOException { 157 super.finish(); 158 159 if (source == null) 160 return; 161 162 final long ipl = source.getPostambleLength(); 163 if (ipl <= 0) 164 return; 165 166 final long il = source.length(); 167 final long ol = length(); 168 169 final InputStream in = source.getPostambleInputStream(); 170 try { 171 if (ol + ipl != il) 177 write(new byte[(int) (ol % 4)]); 178 179 de.schlichtherle.io.File.cat(in, this); 181 } finally { 182 in.close(); 183 } 184 } 185 186 190 public OutputArchiveMetaData getMetaData() { 191 return metaData; 192 } 193 194 public void setMetaData(OutputArchiveMetaData metaData) { 195 this.metaData = metaData; 196 } 197 } 198 | Popular Tags |