1 11 package org.eclipse.ui.internal.wizards.datatransfer; 12 13 import java.io.FileOutputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.util.zip.CRC32 ; 17 import java.util.zip.ZipEntry ; 18 import java.util.zip.ZipOutputStream ; 19 20 import org.eclipse.core.resources.IFile; 21 import org.eclipse.core.runtime.CoreException; 22 23 24 27 public class ZipFileExporter implements IFileExporter { 28 private ZipOutputStream outputStream; 29 30 private boolean useCompression = true; 31 32 39 public ZipFileExporter(String filename, boolean compress) throws IOException { 40 outputStream = new ZipOutputStream (new FileOutputStream (filename)); 41 useCompression = compress; 42 } 43 44 50 public void finished() throws IOException { 51 outputStream.close(); 52 } 53 54 62 private void write(ZipEntry entry, IFile contents) throws IOException , CoreException { 63 byte[] readBuffer = new byte[4096]; 64 65 if (!useCompression) { 67 entry.setMethod(ZipEntry.STORED); 68 InputStream contentStream = contents.getContents(false); 69 int length = 0; 70 CRC32 checksumCalculator = new CRC32 (); 71 try { 72 int n; 73 while ((n = contentStream.read(readBuffer)) > 0) { 74 checksumCalculator.update(readBuffer, 0, n); 75 length += n; 76 } 77 } finally { 78 if (contentStream != null) { 79 contentStream.close(); 80 } 81 } 82 83 entry.setSize(length); 84 entry.setCrc(checksumCalculator.getValue()); 85 } 86 87 outputStream.putNextEntry(entry); 88 InputStream contentStream = contents.getContents(false); 89 try { 90 int n; 91 while ((n = contentStream.read(readBuffer)) > 0) { 92 outputStream.write(readBuffer, 0, n); 93 } 94 } finally { 95 if (contentStream != null) { 96 contentStream.close(); 97 } 98 } 99 outputStream.closeEntry(); 100 } 101 102 110 public void write(IFile resource, String destinationPath) 111 throws IOException , CoreException { 112 ZipEntry newEntry = new ZipEntry (destinationPath); 113 write(newEntry, resource); 114 } 115 } 116 | Popular Tags |