001/*
002 * (C) Copyright 2006-2014 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 *     bstefanescu
018 *
019 */
020
021package org.nuxeo.ecm.core.io.impl.plugins;
022
023import java.io.File;
024import java.io.FileInputStream;
025import java.io.FileOutputStream;
026import java.io.IOException;
027import java.io.InputStream;
028import java.util.zip.ZipEntry;
029import java.util.zip.ZipInputStream;
030
031import org.apache.commons.io.IOUtils;
032import org.nuxeo.ecm.core.io.DocumentReader;
033import org.nuxeo.ecm.core.io.ExportedDocument;
034import org.nuxeo.ecm.core.io.impl.AbstractDocumentReader;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * A reader to read zip files. If the zip file is recognized as a nuxeo archive then the {@link NuxeoArchiveReader} will
039 * be used to read the zip otherwise the zip will be deflated to a temporary directory and then
040 * {@link XMLDirectoryReader} will be used to read the zip.
041 *
042 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
043 */
044public class ZipReader extends AbstractDocumentReader {
045
046    private final ZipInputStream in;
047
048    private DocumentReader delegate;
049
050    public ZipReader(File file) throws IOException {
051        this(new FileInputStream(file));
052    }
053
054    public ZipReader(InputStream in) throws IOException {
055        this.in = new ZipInputStream(in);
056        initialize();
057    }
058
059    private void initialize() throws IOException {
060        ZipEntry entry = in.getNextEntry();
061        if (NuxeoArchiveReader.isMarkerEntry(entry)) {
062            delegate = new NuxeoArchiveReader(in, false);
063        } else { // not a nuxeo archive file
064            File root = null;
065            try {
066                root = Framework.createTempFile("nuxeo-import-", ".unzip");
067                Framework.trackFile(root, root);
068                root.delete();
069                root.mkdirs();
070                extract(in, entry, root);
071                while ((entry = in.getNextEntry()) != null) {
072                    extract(in, entry, root);
073                }
074            } finally {
075                in.close();
076            }
077            delegate = new XMLDirectoryReader(root);
078        }
079    }
080
081    @Override
082    public ExportedDocument read() throws IOException {
083        return delegate.read();
084    }
085
086    @Override
087    public void close() {
088        delegate.close();
089    }
090
091    private static void extract(ZipInputStream in, ZipEntry entry, File root) throws IOException {
092        if (entry.getName().contains("..")) {
093            return;
094        }
095
096        if (!entry.isDirectory()) { // create the directtory
097            File file = new File(root, entry.getName());
098            if (!file.getParentFile().mkdirs()) { // make sure all parent
099                                                  // directory exists
100                throw new IOException("Failed to create directory: " + file.getParent());
101            }
102            // write the file content
103            try (FileOutputStream out = new FileOutputStream(file)) {
104                IOUtils.copy(in, out);
105            }
106        }
107    }
108
109}