KickJava   Java API By Example, From Geeks To Geeks.

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


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

11 package org.eclipse.ui.wizards.datatransfer;
12
13 import java.io.*;
14 import java.security.MessageDigest JavaDoc;
15 import java.security.NoSuchAlgorithmException JavaDoc;
16 import java.util.zip.*;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.CoreException;
20
21 /**
22  * Exports resources to a .zip file
23  */

24 /*package*/ class ZipFileExporter {
25     private ZipOutputStream outputStream;
26     private StringBuffer JavaDoc manifestContents;
27
28     private boolean generateManifestFile = false;
29     private boolean useCompression = true;
30
31     // constants
32
private static final String JavaDoc newline = "\r\n"; // is this platform dependent?//$NON-NLS-1$
33
private static final String JavaDoc manifestMagic = "Manifest-Version: 1.0" + newline + newline;//$NON-NLS-1$
34
private static final String JavaDoc nameLabel = "Name: ";//$NON-NLS-1$
35
private static final String JavaDoc digestsLabel = "Digest-Algorithms: SHA MD5" + newline;//$NON-NLS-1$
36
private static final String JavaDoc shaLabel = "SHA-Digest: ";//$NON-NLS-1$
37
private static final String JavaDoc md5Label = "MD5-Digest: ";//$NON-NLS-1$
38
private static final String JavaDoc manifestPath = "meta-inf/Manifest.mf";//$NON-NLS-1$
39
/**
40  * Create an instance of this class.
41  *
42  * @param filename java.lang.String
43  * @param compress boolean
44  * @param includeManifestFile boolean
45  * @exception java.io.IOException
46  */

47 public ZipFileExporter(String JavaDoc 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 JavaDoc(manifestMagic);
54 }
55 /**
56  * Create a new entry in the manifest file being constructed.
57  *
58  * @param pathname java.lang.String
59  * @param file org.eclipse.core.resources.IFile
60  * @exception java.io.IOException
61  * @exception org.eclipse.core.runtime.CoreException
62  */

63 protected void appendToManifest(String JavaDoc pathname, IFile file) throws IOException, CoreException {
64     StringBuffer JavaDoc manifestEntry = new StringBuffer JavaDoc();
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     // we don't have to EnsureLocal because it was already done in #write
74
InputStream contentStream = file.getContents(false);
75     Reader in = new InputStreamReader(contentStream);
76     int chunkSize = contentStream.available();
77     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(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);//$NON-NLS-1$
89
manifestEntry.append(Base64Encoder.encode(hashValue));
90         manifestEntry.append(newline);
91         manifestEntry.append(md5Label);
92         hashValue = MessageDigest.getInstance("MD5").digest(fileContents);//$NON-NLS-1$
93
manifestEntry.append(Base64Encoder.encode(hashValue));
94         manifestEntry.append(newline + newline);
95     } catch (NoSuchAlgorithmException JavaDoc e) {
96         // should never happen
97
return;
98     }
99     manifestContents.append(manifestEntry.toString());
100 }
101 /**
102  * Do all required cleanup now that we're finished with the
103  * currently-open .zip
104  *
105  * @exception java.io.IOException
106  */

107 public void finished() throws IOException {
108     if (generateManifestFile)
109         writeManifestFile();
110         
111     outputStream.close();
112 }
113 /**
114  * Create a new ZipEntry with the passed pathname and contents, and write it
115  * to the current archive
116  *
117  * @param pathname java.lang.String
118  * @param contents byte[]
119  * @exception java.io.IOException
120  */

121 protected void write(String JavaDoc pathname, byte[] contents) throws IOException {
122     ZipEntry newEntry = new ZipEntry(pathname);
123
124     // if the contents are being compressed then we get the below for free.
125
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 /**
138  * Write the passed resource to the current archive
139  *
140  * @param resource org.eclipse.core.resources.IFile
141  * @param destinationPath java.lang.String
142  * @exception java.io.IOException
143  * @exception org.eclipse.core.runtime.CoreException
144  */

145 public void write(IFile resource, String JavaDoc 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 /**
172  * Write the constructed manifest.mf file to the current archive
173  *
174  * @exception java.io.IOException
175  */

176 protected void writeManifestFile() throws IOException {
177     write(manifestPath,manifestContents.toString().getBytes());
178 }
179 }
180
Popular Tags