001/*
002 * Copyright (c) 2006-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Bogdan Stefanescu
011 *     Florent Guillaume
012 */
013package org.nuxeo.ecm.core.api.impl.blob;
014
015import java.io.IOException;
016import java.io.InputStream;
017import java.io.Serializable;
018import java.util.zip.ZipEntry;
019import java.util.zip.ZipFile;
020
021import org.nuxeo.ecm.core.api.Blob;
022
023/**
024 * A {@link Blob} backed by an entry in a ZIP file.
025 *
026 * @since 7.2
027 */
028public class ZipEntryBlob extends AbstractBlob implements Serializable {
029
030    private static final long serialVersionUID = 1L;
031
032    protected final ZipFile zipFile;
033
034    protected final ZipEntry zipEntry;
035
036    /**
037     * Creates a {@link Blob} from an entry in a zip file. The {@link ZipFile} must not be closed until the stream has
038     * been read.
039     *
040     * @param zipFile the zip file
041     * @param zipEntry the zip entry
042     */
043    public ZipEntryBlob(ZipFile zipFile, ZipEntry zipEntry) {
044        this.zipFile = zipFile;
045        this.zipEntry = zipEntry;
046    }
047
048    @Override
049    public InputStream getStream() throws IOException {
050        return zipFile.getInputStream(zipEntry);
051    }
052
053    @Override
054    public long getLength() {
055        return zipEntry.getSize();
056    }
057
058}