001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.scan;
023
024import java.io.File;
025import java.io.IOException;
026import java.io.InputStream;
027import java.util.zip.ZipEntry;
028import java.util.zip.ZipFile;
029
030/**
031 * @author Franck WOLFF
032 */
033public class ZipScannedItem extends AbstractScannedItem {
034
035    private final ZipFile file;
036    private final ZipEntry entry;
037
038    public ZipScannedItem(Scanner scanner, ZipScannedItem marker, ZipFile file, ZipEntry entry) {
039        super(scanner, marker);
040
041        this.file = file;
042        this.entry = entry;
043    }
044
045    public ZipFile getFile() {
046        return file;
047    }
048
049    public ZipEntry getEntry() {
050        return entry;
051    }
052
053    public String getName() {
054        String path = entry.getName();
055        int lastSlash = path.lastIndexOf('/');
056        return (lastSlash >= 0 ? path.substring(lastSlash + 1) : path);
057    }
058
059    public String getRelativePath() {
060        return entry.getName();
061    }
062
063    public String getAbsolutePath() {
064        return new File(file.getName()).getAbsolutePath() + '!' + entry.getName();
065    }
066
067    public long getSize() {
068        return entry.getSize();
069    }
070
071    public InputStream getInputStream() throws IOException {
072        return file.getInputStream(entry);
073    }
074
075    @Override
076    public boolean equals(Object obj) {
077        if (obj == this)
078            return true;
079        if (!(obj instanceof ZipScannedItem))
080            return false;
081        return file.equals(((ZipScannedItem)obj).file) && entry.equals(((ZipScannedItem)obj).entry);
082    }
083
084    @Override
085    public int hashCode() {
086        return file.hashCode() + (31 * entry.hashCode());
087    }
088}