001/*
002 * (C) Copyright 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     Bogdan Stefanescu
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.core.api.impl.blob;
021
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.Serializable;
025import java.util.zip.ZipEntry;
026import java.util.zip.ZipFile;
027
028import org.nuxeo.ecm.core.api.Blob;
029
030/**
031 * A {@link Blob} backed by an entry in a ZIP file.
032 *
033 * @since 7.2
034 */
035public class ZipEntryBlob extends AbstractBlob implements Serializable {
036
037    private static final long serialVersionUID = 1L;
038
039    protected final ZipFile zipFile;
040
041    protected final ZipEntry zipEntry;
042
043    /**
044     * Creates a {@link Blob} from an entry in a zip file. The {@link ZipFile} must not be closed until the stream has
045     * been read.
046     *
047     * @param zipFile the zip file
048     * @param zipEntry the zip entry
049     */
050    public ZipEntryBlob(ZipFile zipFile, ZipEntry zipEntry) {
051        this.zipFile = zipFile;
052        this.zipEntry = zipEntry;
053    }
054
055    @Override
056    public InputStream getStream() throws IOException {
057        return zipFile.getInputStream(zipEntry);
058    }
059
060    @Override
061    public long getLength() {
062        return zipEntry.getSize();
063    }
064
065}