001/*-------------------------------------------------------------------------+
002|                                                                          |
003| Copyright 2005-2011 the ConQAT Project                                   |
004|                                                                          |
005| Licensed under the Apache License, Version 2.0 (the "License");          |
006| you may not use this file except in compliance with the License.         |
007| You may obtain a copy of the License at                                  |
008|                                                                          |
009|    http://www.apache.org/licenses/LICENSE-2.0                            |
010|                                                                          |
011| Unless required by applicable law or agreed to in writing, software      |
012| distributed under the License is distributed on an "AS IS" BASIS,        |
013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014| See the License for the specific language governing permissions and      |
015| limitations under the License.                                           |
016+-------------------------------------------------------------------------*/
017package org.conqat.lib.commons.filesystem;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022import org.apache.commons.compress.archivers.ArchiveEntry;
023import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
024import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
025import org.conqat.lib.commons.string.StringUtils;
026
027/**
028 * Utility methods which deal with the content of Zip files. (Does not support
029 * caching.)
030 */
031public abstract class ZipFileUtils {
032
033        /**
034         * Reads the content of an entry in a ZipFile.
035         *
036         * @throws IOException
037         *             if no entry with the given name exists or no size information is
038         *             stored for the entry.
039         */
040        public static byte[] readZipEntryContent(ZipFile zipFile, String entryName) throws IOException {
041                ZipArchiveEntry entry = zipFile.getEntry(entryName);
042                if (entry == null) {
043                        throw new IOException("Entry " + entryName + " does not exist in " + zipFile.getName());
044                }
045                int size = (int) entry.getSize();
046                if (size < 0) {
047                        throw new IOException("Size for entry " + entryName + " not stored in ZIP file " + zipFile.getName());
048                }
049                byte[] content = new byte[size];
050
051                try (InputStream in = zipFile.getInputStream(entry)) {
052                        FileSystemUtils.safeRead(in, content);
053                        return content;
054                }
055        }
056
057        /** @return The entry for the given entry name as a {@link String} or null. */
058        public static String extractEntryAsString(ZipArchiveInputStream zis, String entryName) throws IOException {
059                ArchiveEntry entry;
060                while ((entry = zis.getNextEntry()) != null) {
061                        if (entry.getName().equals(entryName)) {
062                                byte[] content = FileSystemUtils.readStreamBinary(zis);
063                                return StringUtils.bytesToString(content);
064                        }
065                }
066                return null;
067        }
068}