KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > datatransfer > ZipFileExporter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.wizards.datatransfer;
12
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.util.zip.CRC32 JavaDoc;
17 import java.util.zip.ZipEntry JavaDoc;
18 import java.util.zip.ZipOutputStream JavaDoc;
19
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.runtime.CoreException;
22
23
24 /**
25  * Exports resources to a .zip file
26  */

27 public class ZipFileExporter implements IFileExporter {
28     private ZipOutputStream JavaDoc outputStream;
29
30     private boolean useCompression = true;
31
32     /**
33      * Create an instance of this class.
34      *
35      * @param filename java.lang.String
36      * @param compress boolean
37      * @exception java.io.IOException
38      */

39     public ZipFileExporter(String JavaDoc filename, boolean compress) throws IOException JavaDoc {
40         outputStream = new ZipOutputStream JavaDoc(new FileOutputStream JavaDoc(filename));
41         useCompression = compress;
42     }
43
44     /**
45      * Do all required cleanup now that we're finished with the
46      * currently-open .zip
47      *
48      * @exception java.io.IOException
49      */

50     public void finished() throws IOException JavaDoc {
51         outputStream.close();
52     }
53
54     /**
55      * Write the contents of the file to the tar archive.
56      *
57      * @param entry
58      * @param contents
59      * @exception java.io.IOException
60      * @exception org.eclipse.core.runtime.CoreException
61      */

62     private void write(ZipEntry JavaDoc entry, IFile contents) throws IOException JavaDoc, CoreException {
63         byte[] readBuffer = new byte[4096];
64
65         // If the contents are being compressed then we get the below for free.
66
if (!useCompression) {
67             entry.setMethod(ZipEntry.STORED);
68             InputStream JavaDoc contentStream = contents.getContents(false);
69             int length = 0;
70             CRC32 JavaDoc checksumCalculator = new CRC32 JavaDoc();
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 JavaDoc 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     /**
103      * Write the passed resource to the current archive.
104      *
105      * @param resource org.eclipse.core.resources.IFile
106      * @param destinationPath java.lang.String
107      * @exception java.io.IOException
108      * @exception org.eclipse.core.runtime.CoreException
109      */

110     public void write(IFile resource, String JavaDoc destinationPath)
111             throws IOException JavaDoc, CoreException {
112         ZipEntry JavaDoc newEntry = new ZipEntry JavaDoc(destinationPath);
113         write(newEntry, resource);
114     }
115 }
116
Popular Tags