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