All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jackrabbit.vault.fs.io.ZipArchive Maven / Gradle / Ivy

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jackrabbit.vault.fs.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.vault.fs.api.VaultInputSource;
import org.apache.jackrabbit.vault.fs.config.ConfigurationException;
import org.apache.jackrabbit.vault.fs.config.DefaultMetaInf;
import org.apache.jackrabbit.vault.fs.config.MetaInf;
import org.apache.jackrabbit.vault.fs.config.VaultSettings;
import org.apache.jackrabbit.vault.util.Constants;
import org.apache.jackrabbit.vault.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Implements an archive that is based on a zip file.
 */
public class ZipArchive extends AbstractArchive {

    /**
     * default logger
     */
    private static final Logger log = LoggerFactory.getLogger(ZipArchive.class);

    /**
     * the zip file
     */
    private final File file;

    /**
     * {@code true} if file is temporary and can be deleted after this archive is closed.
     */
    private final boolean isTempFile;

    /**
     * The (loaded) meta info
     */
    private DefaultMetaInf inf;

    /**
     * the jar file that is created upon {@link #open(boolean)}
     */
    private JarFile jar;

    /**
     * the root entry of this archive
     */
    private EntryImpl root;

    /**
     * Creates a new archive that is based on the given zip file.
     * @param zipFile the zip file
     */
    public ZipArchive(@Nonnull File zipFile) {
        this(zipFile, false);
    }

    /**
     * Creates a new archive that is based on the given zip file.
     * @param zipFile the zip file
     * @param isTempFile if {@code true} if the file is considered temporary and can be deleted after this archive is closed.
     */
    public ZipArchive(@Nonnull File zipFile, boolean isTempFile) {
        this.file = zipFile;
        this.isTempFile = isTempFile;
    }

    @Override
    public void open(boolean strict) throws IOException {
        if (jar != null) {
            return;
        }
        jar = new JarFile(file);
        root = new EntryImpl("", true);
        inf = new DefaultMetaInf();

        Enumeration e = jar.entries();
        while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            String path = entry.getName();
            // check for meta inf
            if (path.startsWith(Constants.META_DIR + "/")) {
                try {
                    inf.load(jar.getInputStream(entry), file.getPath() + ":" + path);
                } catch (ConfigurationException e1) {
                    throw new IOException(e1);
                }
            }
            String[] names = Text.explode(path, '/');
            if (names.length > 0) {
                EntryImpl je = root;
                for (int i=0; i children;

        private EntryImpl(@Nonnull String name, boolean directory) {
            this.name = name;
            isDirectory = directory;
        }

        @Nonnull
        private EntryImpl add(@Nonnull EntryImpl e) {
            if (children == null) {
                children = new LinkedHashMap();
            }
            children.put(e.getName(), e);
            return e;
        }

        @Nonnull
        public EntryImpl add(@Nonnull String name, boolean isDirectory) {
            if (children != null) {
                EntryImpl ret = children.get(name);
                if (ret != null) {
                    return ret;
                }
            }
            return add(new EntryImpl(name, isDirectory));
        }

        @Override
        @Nonnull
        public String getName() {
            return name;
        }

        @Override
        public boolean isDirectory() {
            return isDirectory;
        }

        @Override
        @Nonnull
        public Collection getChildren() {
            return children == null
                    ? Collections.emptyList()
                    : children.values();
        }

        @Override
        @Nullable
        public Entry getChild(@Nonnull String name) {
            return children == null ? null : children.get(name);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy