1 11 package org.eclipse.ui.internal.ide.dialogs; 12 13 import java.io.*; 14 import java.security.MessageDigest ; 15 import java.security.NoSuchAlgorithmException ; 16 import java.util.zip.*; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.runtime.CoreException; 20 21 24 class ZipFileResourceExporter { 25 private ZipOutputStream outputStream; 26 private StringBuffer manifestContents; 27 28 private boolean generateManifestFile = false; 29 private boolean useCompression = true; 30 31 private static final String newline = "\r\n"; private static final String manifestMagic = "Manifest-Version: 1.0" + newline + newline; private static final String nameLabel = "Name: "; private static final String digestsLabel = "Digest-Algorithms: SHA MD5" + newline; private static final String shaLabel = "SHA-Digest: "; private static final String md5Label = "MD5-Digest: "; private static final String manifestPath = "meta-inf/Manifest.mf"; 47 public ZipFileResourceExporter(String filename,boolean compress,boolean includeManifestFile) throws IOException { 48 outputStream = new ZipOutputStream(new FileOutputStream(filename)); 49 useCompression = compress; 50 generateManifestFile = includeManifestFile; 51 52 if (generateManifestFile) 53 manifestContents = new StringBuffer (manifestMagic); 54 } 55 63 protected void appendToManifest(String pathname, IFile file) throws IOException, CoreException { 64 StringBuffer manifestEntry = new StringBuffer (); 65 manifestEntry.append(nameLabel); 66 manifestEntry.append(pathname); 67 manifestEntry.append(newline); 68 manifestEntry.append(digestsLabel); 69 manifestEntry.append(shaLabel); 70 71 byte[] fileContents = null; 72 73 InputStream contentStream = file.getContents(false); 75 Reader in = new InputStreamReader(contentStream); 76 int chunkSize = contentStream.available(); 77 StringBuffer buffer = new StringBuffer (chunkSize); 78 char[] readBuffer = new char[chunkSize]; 79 int n = in.read(readBuffer); 80 while (n > 0) { 81 buffer.append(readBuffer); 82 n = in.read(readBuffer); 83 } 84 contentStream.close(); 85 fileContents = buffer.toString().getBytes(); 86 87 try { 88 byte[] hashValue = MessageDigest.getInstance("SHA").digest(fileContents); manifestEntry.append(InternalBase64Encoder.encode(hashValue)); 90 manifestEntry.append(newline); 91 manifestEntry.append(md5Label); 92 hashValue = MessageDigest.getInstance("MD5").digest(fileContents); manifestEntry.append(InternalBase64Encoder.encode(hashValue)); 94 manifestEntry.append(newline + newline); 95 } catch (NoSuchAlgorithmException e) { 96 return; 98 } 99 manifestContents.append(manifestEntry.toString()); 100 } 101 107 public void finished() throws IOException { 108 if (generateManifestFile) 109 writeManifestFile(); 110 111 outputStream.close(); 112 } 113 121 protected void write(String pathname, byte[] contents) throws IOException { 122 ZipEntry newEntry = new ZipEntry(pathname); 123 124 if (!useCompression) { 126 newEntry.setMethod(ZipEntry.STORED); 127 newEntry.setSize(contents.length); 128 CRC32 checksumCalculator = new CRC32(); 129 checksumCalculator.update(contents); 130 newEntry.setCrc(checksumCalculator.getValue()); 131 } 132 133 outputStream.putNextEntry(newEntry); 134 outputStream.write(contents); 135 outputStream.closeEntry(); 136 } 137 145 public void write(IFile resource, String destinationPath) throws IOException, CoreException { 146 ByteArrayOutputStream output = null; 147 InputStream contentStream = null; 148 149 try { 150 output = new ByteArrayOutputStream(); 151 contentStream = resource.getContents(false); 152 int chunkSize = contentStream.available(); 153 byte[] readBuffer = new byte[chunkSize]; 154 int n = contentStream.read(readBuffer); 155 156 while (n > 0) { 157 output.write(readBuffer); 158 n = contentStream.read(readBuffer); 159 } 160 } finally { 161 if (output != null) 162 output.close(); 163 if (contentStream != null) 164 contentStream.close(); 165 } 166 167 write(destinationPath,output.toByteArray()); 168 if (generateManifestFile) 169 appendToManifest(destinationPath, resource); 170 } 171 176 protected void writeManifestFile() throws IOException { 177 write(manifestPath,manifestContents.toString().getBytes()); 178 } 179 } 180 | Popular Tags |