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

org.gradle.api.plugins.buildcomparison.outcome.internal.archive.entry.FileToArchiveEntrySetTransformer Maven / Gradle / Ivy

There is a newer version: 8.6
Show newest version
/*
 * Copyright 2012 the original author or authors.
 *
 * Licensed 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.gradle.api.plugins.buildcomparison.outcome.internal.archive.entry;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.gradle.api.Transformer;
import org.gradle.api.UncheckedIOException;
import org.gradle.internal.IoActions;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class FileToArchiveEntrySetTransformer implements Transformer, File> {

    public Set transform(File archiveFile) {
        FileInputStream fileInputStream;
        try {
            fileInputStream = new FileInputStream(archiveFile);
        } catch (FileNotFoundException e) {
            throw new UncheckedIOException(e);
        }

        ImmutableSet.Builder allEntries = ImmutableSet.builder();
        walk(fileInputStream, allEntries, ImmutableList.of());
        return allEntries.build();
    }

    private ImmutableSet walk(InputStream archiveInputStream, ImmutableSet.Builder allEntries, ImmutableList parentPaths) {
        ImmutableSet.Builder entries = ImmutableSet.builder();
        ZipInputStream zipStream = new ZipInputStream(archiveInputStream);

        try {
            ZipEntry entry = zipStream.getNextEntry();
            while (entry != null) {
                ArchiveEntry.Builder builder = new ArchiveEntry.Builder();
                builder.setParentPaths(parentPaths);
                builder.setPath(entry.getName());
                builder.setCrc(entry.getCrc());
                builder.setDirectory(entry.isDirectory());
                builder.setSize(entry.getSize());
                if (!builder.isDirectory() && (zipStream.available() == 1)) {
                    boolean zipEntry;
                    final BufferedInputStream bis = new BufferedInputStream(zipStream) {
                        @Override
                        public void close() throws IOException {
                        }
                    };
                    bis.mark(Integer.MAX_VALUE);
                    zipEntry = new ZipInputStream(bis).getNextEntry() != null;
                    bis.reset();
                    if (zipEntry) {
                        ImmutableList nextParentPaths = ImmutableList.builder().addAll(parentPaths).add(entry.getName()).build();
                        ImmutableSet subEntries = walk(bis, allEntries, nextParentPaths);
                        builder.setSubEntries(subEntries);
                    }
                }

                ArchiveEntry archiveEntry = builder.build();
                entries.add(archiveEntry);
                allEntries.add(archiveEntry);
                zipStream.closeEntry();
                entry = zipStream.getNextEntry();
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } finally {
            IoActions.closeQuietly(zipStream);
        }

        return entries.build();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy