001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.wizards.datatransfer;
011:
012: import java.io.FileOutputStream;
013: import java.io.IOException;
014: import java.io.InputStream;
015: import java.util.zip.CRC32;
016: import java.util.zip.ZipEntry;
017: import java.util.zip.ZipOutputStream;
018:
019: import org.eclipse.core.resources.IFile;
020: import org.eclipse.core.runtime.CoreException;
021:
022: /**
023: * Exports resources to a .zip file
024: */
025: public class ZipFileExporter implements IFileExporter {
026: private ZipOutputStream outputStream;
027:
028: private boolean useCompression = true;
029:
030: /**
031: * Create an instance of this class.
032: *
033: * @param filename java.lang.String
034: * @param compress boolean
035: * @exception java.io.IOException
036: */
037: public ZipFileExporter(String filename, boolean compress)
038: throws IOException {
039: outputStream = new ZipOutputStream(new FileOutputStream(
040: filename));
041: useCompression = compress;
042: }
043:
044: /**
045: * Do all required cleanup now that we're finished with the
046: * currently-open .zip
047: *
048: * @exception java.io.IOException
049: */
050: public void finished() throws IOException {
051: outputStream.close();
052: }
053:
054: /**
055: * Write the contents of the file to the tar archive.
056: *
057: * @param entry
058: * @param contents
059: * @exception java.io.IOException
060: * @exception org.eclipse.core.runtime.CoreException
061: */
062: private void write(ZipEntry entry, IFile contents)
063: throws IOException, CoreException {
064: byte[] readBuffer = new byte[4096];
065:
066: // If the contents are being compressed then we get the below for free.
067: if (!useCompression) {
068: entry.setMethod(ZipEntry.STORED);
069: InputStream contentStream = contents.getContents(false);
070: int length = 0;
071: CRC32 checksumCalculator = new CRC32();
072: try {
073: int n;
074: while ((n = contentStream.read(readBuffer)) > 0) {
075: checksumCalculator.update(readBuffer, 0, n);
076: length += n;
077: }
078: } finally {
079: if (contentStream != null) {
080: contentStream.close();
081: }
082: }
083:
084: entry.setSize(length);
085: entry.setCrc(checksumCalculator.getValue());
086: }
087:
088: outputStream.putNextEntry(entry);
089: InputStream contentStream = contents.getContents(false);
090: try {
091: int n;
092: while ((n = contentStream.read(readBuffer)) > 0) {
093: outputStream.write(readBuffer, 0, n);
094: }
095: } finally {
096: if (contentStream != null) {
097: contentStream.close();
098: }
099: }
100: outputStream.closeEntry();
101: }
102:
103: /**
104: * Write the passed resource to the current archive.
105: *
106: * @param resource org.eclipse.core.resources.IFile
107: * @param destinationPath java.lang.String
108: * @exception java.io.IOException
109: * @exception org.eclipse.core.runtime.CoreException
110: */
111: public void write(IFile resource, String destinationPath)
112: throws IOException, CoreException {
113: ZipEntry newEntry = new ZipEntry(destinationPath);
114: write(newEntry, resource);
115: }
116: }
|