01: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
02: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
03: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
04: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
05: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
06: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
07: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
08: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
09: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
11: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
12: // POSSIBILITY OF SUCH DAMAGE.
13: //
14: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
15: package com.metaboss.util;
16:
17: import java.io.ByteArrayOutputStream;
18: import java.io.FileInputStream;
19: import java.io.FileNotFoundException;
20: import java.io.IOException;
21: import java.util.zip.ZipEntry;
22: import java.util.zip.ZipOutputStream;
23:
24: /** Set of useful utilites to do with zipping and unzipping the information*/
25: public class ZipUtils {
26: /** Gets the contents of the specified directory, zips it up and returns the result as the bte array. */
27: public static byte[] zipUpDirectory(
28: String pSourceDirectoryAbsolutePath)
29: throws FileNotFoundException, IOException {
30: ByteArrayOutputStream lOutputStream = new ByteArrayOutputStream();
31: ZipOutputStream lZipStream = null;
32: try {
33: lZipStream = new ZipOutputStream(lOutputStream);
34: String[] lFilesToZip = DirectoryUtils
35: .listAllFilesInDirectory(pSourceDirectoryAbsolutePath);
36: for (int i = 0; i < lFilesToZip.length; i++) {
37: String lAbsoluteFileName = lFilesToZip[i];
38: String lRelativeFileName = lAbsoluteFileName
39: .substring(pSourceDirectoryAbsolutePath
40: .length());
41: if (lRelativeFileName.startsWith("\\"))
42: lRelativeFileName = lRelativeFileName.substring(1);
43: ZipEntry lZipEntry = new ZipEntry(lRelativeFileName);
44: lZipStream.putNextEntry(lZipEntry);
45:
46: FileInputStream lFileInputStream = new FileInputStream(
47: lAbsoluteFileName);
48: try {
49: byte buffer[] = new byte[1024];
50: int bytesRead;
51: while ((bytesRead = lFileInputStream.read(buffer)) != -1)
52: lZipStream.write(buffer, 0, bytesRead);
53: } finally {
54: lFileInputStream.close();
55: }
56: }
57: lZipStream.flush();
58: lZipStream.close();
59: lZipStream = null;
60: lOutputStream.flush();
61: return lOutputStream.toByteArray();
62: } finally {
63: if (lZipStream != null) {
64: try {
65: lZipStream.close();
66: } catch (IOException e) {
67: // Ignore
68: }
69: }
70: }
71: }
72: }
|