001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.zip;
022:
023: import com.liferay.portal.kernel.log.Log;
024: import com.liferay.portal.kernel.log.LogFactoryUtil;
025: import com.liferay.portal.kernel.util.ByteArrayMaker;
026: import com.liferay.portal.kernel.util.ObjectValuePair;
027: import com.liferay.portal.kernel.util.StringPool;
028:
029: import java.io.File;
030: import java.io.FileInputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.Serializable;
034:
035: import java.util.ArrayList;
036: import java.util.LinkedHashMap;
037: import java.util.List;
038: import java.util.Map;
039: import java.util.zip.ZipEntry;
040: import java.util.zip.ZipInputStream;
041:
042: /**
043: * <a href="ZipReader.java.html"><b><i>View Source</i></b></a>
044: *
045: * @author Alexander Chow
046: * @author Brian Wing Shun Chan
047: *
048: */
049: public class ZipReader implements Serializable {
050:
051: public ZipReader(File file) throws Exception {
052: _zis = new ZipInputStream(new FileInputStream(file));
053: }
054:
055: public ZipReader(InputStream stream) {
056: try {
057: _zis = new ZipInputStream(stream);
058:
059: while (true) {
060: ZipEntry entry = _zis.getNextEntry();
061:
062: if (entry == null) {
063: break;
064: }
065:
066: String currentName = entry.getName();
067:
068: ByteArrayMaker bam = new ByteArrayMaker();
069:
070: while (true) {
071: int count = _zis.read(_data, 0, _BUFFER);
072:
073: if (count == -1) {
074: break;
075: }
076:
077: bam.write(_data, 0, count);
078: }
079:
080: byte[] byteArray = bam.toByteArray();
081:
082: _entries.put(currentName, byteArray);
083:
084: int pos = currentName.lastIndexOf(StringPool.SLASH);
085:
086: String folderPath = StringPool.BLANK;
087: String fileName = currentName;
088:
089: if (pos > 0) {
090: folderPath = currentName.substring(0, pos + 1);
091: fileName = currentName.substring(pos + 1);
092: }
093:
094: List files = (List) _folderEntries.get(folderPath);
095:
096: if (files == null) {
097: files = new ArrayList();
098:
099: _folderEntries.put(folderPath, files);
100: }
101:
102: files.add(new ObjectValuePair(fileName, byteArray));
103: }
104: } catch (IOException ioe) {
105: _log.error(ioe, ioe);
106: } finally {
107: try {
108: _zis.close();
109: } catch (Exception e) {
110: if (_log.isWarnEnabled()) {
111: _log.warn(e);
112: }
113: }
114: }
115: }
116:
117: public Map getEntries() {
118: return _entries;
119: }
120:
121: public String getEntryAsString(String name) {
122: byte[] byteArray = getEntryAsByteArray(name);
123:
124: if (byteArray != null) {
125: return new String(byteArray);
126: }
127:
128: return null;
129: }
130:
131: public byte[] getEntryAsByteArray(String name) {
132: return (byte[]) _entries.get(name);
133: }
134:
135: public Map getFolderEntries() {
136: return _folderEntries;
137: }
138:
139: private static final int _BUFFER = 2048;
140:
141: private static Log _log = LogFactoryUtil.getLog(ZipReader.class);
142:
143: private ZipInputStream _zis;
144: private Map _entries = new LinkedHashMap();
145: private Map _folderEntries = new LinkedHashMap();
146: private byte[] _data = new byte[_BUFFER];
147:
148: }
|