001/*
002 * Copyright 2011-2012 Stephen Connolly.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.jszip.pseudo.io;
018
019import org.codehaus.plexus.archiver.zip.ZipEntry;
020import org.codehaus.plexus.archiver.zip.ZipFile;
021import org.codehaus.plexus.util.IOUtil;
022
023import java.io.ByteArrayInputStream;
024import java.io.File;
025import java.io.IOException;
026import java.io.InputStream;
027import java.io.OutputStream;
028
029public class ZipPseudoFile extends PseudoFile {
030
031    private final File zipFile;
032    private final ZipEntry entry;
033
034    public ZipPseudoFile(PseudoFile parent, File zipFile, ZipEntry entry) {
035        super(parent);
036        this.zipFile = zipFile;
037        this.entry = entry;
038    }
039
040    /**
041     * {@inheritDoc}
042     */
043    public boolean canRead() {
044        return true;
045    }
046
047    /**
048     * {@inheritDoc}
049     */
050    public boolean canWrite() {
051        return false;
052    }
053
054    /**
055     * {@inheritDoc}
056     */
057    public boolean createNewFile() throws IOException {
058        throw new IOException(getPath() + " is read-only");
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    public boolean delete() {
065        return false;
066    }
067
068    /**
069     * {@inheritDoc}
070     */
071    public void deleteOnExit() {
072    }
073
074    /**
075     * {@inheritDoc}
076     */
077    public boolean exists() {
078        return true;
079    }
080
081    /**
082     * {@inheritDoc}
083     */
084    public String getName() {
085        final String name = entry.getName();
086        int index = name.lastIndexOf('/');
087        if (index == -1) {
088            return name;
089        }
090        return name.substring(index + 1);
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    public boolean isDirectory() {
097        return entry.isDirectory();
098    }
099
100    /**
101     * {@inheritDoc}
102     */
103    public boolean isFile() {
104        return !entry.isDirectory();
105    }
106
107    /**
108     * {@inheritDoc}
109     */
110    public boolean isHidden() {
111        return false;
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    public long lastModified() {
118        return entry.getTime();
119    }
120
121    /**
122     * {@inheritDoc}
123     */
124    public long length() {
125        return entry.getSize();
126    }
127
128    /**
129     * {@inheritDoc}
130     */
131    public boolean mkdir() {
132        return false;
133    }
134
135    /**
136     * {@inheritDoc}
137     */
138    public boolean mkdirs() {
139        getParentFile().mkdirs();
140        return false;
141    }
142
143    /**
144     * {@inheritDoc}
145     */
146    public boolean renameTo(PseudoFile dest) {
147        return false;
148    }
149
150    /**
151     * {@inheritDoc}
152     */
153    public boolean setLastModified(long time) {
154        return false;
155    }
156
157    /**
158     * {@inheritDoc}
159     */
160    InputStream $newInputStream() throws IOException {
161        InputStream inputStream = null;
162        ZipFile zipFile = null;
163        try {
164            zipFile = new ZipFile(this.zipFile);
165            inputStream = zipFile.getInputStream(zipFile.getEntry(entry.getName()));
166            byte[] contents = IOUtil.toByteArray(inputStream);
167            return new ByteArrayInputStream(contents);
168        } finally {
169            if (zipFile != null) {
170                try {
171                    zipFile.close();
172                } catch (IOException e) {
173                    // ignore
174                }
175            }
176            IOUtil.close(inputStream);
177        }
178    }
179
180    /**
181     * {@inheritDoc}
182     */
183    OutputStream $newOutputStream() throws IOException {
184        throw new IOException(getPath() + " is read-only");
185    }
186
187    /**
188     * {@inheritDoc}
189     */
190    OutputStream $newOutputStream(boolean append) throws IOException {
191        throw new IOException(getPath() + " is read-only");
192    }
193}