001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.zip;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncFilterInputStream;
018    
019    import java.io.File;
020    import java.io.IOException;
021    import java.io.InputStream;
022    
023    import java.util.zip.ZipFile;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class ZipFileUtil {
029    
030            public static InputStream openInputStream(File file, String entryName)
031                    throws IOException {
032    
033                    ZipFile zipFile = new ZipFile(file);
034    
035                    return new ZipFileInputStream(
036                            zipFile.getInputStream(zipFile.getEntry(entryName)), zipFile);
037            }
038    
039            private static class ZipFileInputStream extends UnsyncFilterInputStream {
040    
041                    @Override
042                    public void close() throws IOException {
043                            try {
044                                    inputStream.close();
045                            }
046                            finally {
047                                    _zipFile.close();
048                            }
049                    }
050    
051                    private ZipFileInputStream(InputStream inputStream, ZipFile zipFile) {
052                            super(inputStream);
053    
054                            _zipFile = zipFile;
055                    }
056    
057                    private final ZipFile _zipFile;
058    
059            }
060    
061    }