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

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

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
/*
 * 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.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

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.h2.util.CloseWatcher;
import org.apache.jackrabbit.util.Text;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
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;

    /** The watcher for unclosed archives */
    private CloseWatcher watcher;

    /**
     * Creates a new archive that is based on the given zip file.
     * @param zipFile the zip file
     */
    public ZipArchive(@NotNull 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(@NotNull 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 = e.nextElement();
            String path = entry.getName();
            // check for meta inf
            if (path.startsWith(Constants.META_DIR + "/")) {
                try (InputStream input = jar.getInputStream(entry)) {
                    inf.load(input, 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(@NotNull String name, boolean directory) {
            this.name = name;
            isDirectory = directory;
        }

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

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

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

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

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

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

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy