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

br.com.objectos.io.Unzip Maven / Gradle / Ivy

/*
 * Copyright (C) 2011-2021 Objectos Software LTDA.
 *
 * 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 br.com.objectos.io;

import static br.com.objectos.collections.Collections.newGrowableList;
import static br.com.objectos.lang.Lang.checkNotNull;

import br.com.objectos.collections.GrowableList;
import br.com.objectos.lang.Lang;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

final class Unzip {

  private final byte[] buffer;

  private final Enumeration entries;

  private IOException ioException;

  private final Directory workingDirectory;

  private final ZipFile zip;

  Unzip(byte[] buffer,
        Directory workingDirectory,
        ZipFile zip) {
    this.buffer = buffer;
    this.workingDirectory = workingDirectory;
    this.zip = zip;

    entries = zip.entries();
  }

  public static void unzip(
      Directory workingDirectory, RegularFile zipFile)
      throws IOException {
    checkNotNull(workingDirectory, "workingDirectory == null");
    checkNotNull(zipFile, "zipFile == null");

    File file;
    file = zipFile.toFile();

    ZipFile zip;
    zip = new ZipFile(file);

    byte[] buffer;
    buffer = Io.createBuffer();

    Unzip unzip;
    unzip = new Unzip(buffer, workingDirectory, zip);

    unzip.execute();
  }

  public final void execute() throws IOException {
    try {
      execute0();
    } catch (IOException e) {
      ioException = e;
    } finally {
      ioException = Lang.close(ioException, zip);
    }

    Lang.rethrowIfNecessary(ioException);
  }

  private void execute0() throws IOException {
    if (!entries.hasMoreElements()) {
      return;
    }

    StringBuilder namePart;
    namePart = new StringBuilder();

    GrowableList nameParts;
    nameParts = newGrowableList();

    while (entries.hasMoreElements()) {
      ZipEntry entry;
      entry = entries.nextElement();

      String entryName;
      entryName = entry.getName();

      if (entryName.isEmpty()) {
        ioException = new IOException("Cannot unzip, entry pathname is empty");

        break;
      }

      nameParts.clear();

      char[] entryArray;
      entryArray = entryName.toCharArray();

      char c;
      c = entryArray[0];

      if (c == '/') {
        ioException = new IOException("Cannot unzip, entry pathname is absolute: " + entryName);

        break;
      }

      namePart.setLength(0);

      namePart.append(c);

      for (int i = 1, length = entryArray.length; i < length; i++) {
        c = entryArray[i];

        switch (c) {
          case '/':
          case '\\':
            String s;
            s = namePart.toString();

            nameParts.add(s);

            namePart.setLength(0);

            break;
          default:
            namePart.append(c);

            break;
        }
      }

      if (namePart.length() > 0) {
        String s;
        s = namePart.toString();

        nameParts.add(s);

        namePart.setLength(0);
      }

      if (nameParts.isEmpty()) {
        ioException = new IOException("Cannot unzip, entry pathname is empty");

        break;
      }

      String firstName;
      firstName = nameParts.get(0);

      String[] moreNames;
      moreNames = new String[nameParts.size() - 1];

      for (int i = 1, size = nameParts.size(); i < size; i++) {
        moreNames[i - 1] = nameParts.get(i);
      }

      FsObject object;
      object = workingDirectory.resolve(firstName, moreNames);

      if (!object.isNotFound()) {
        ioException = new IOException("Cannot unzip, destination exists: " + object.getPath());

        break;
      }

      NotFound notFound;
      notFound = object.toNotFound();

      notFound.createParents();

      if (entry.isDirectory()) {
        notFound.createDirectory();

        continue;
      }

      InputStream in;
      in = zip.getInputStream(entry);

      OutputStream out;
      out = null;

      try {
        out = notFound.openOutputStream();

        Io.copy(in, out, buffer);
      } catch (IOException e) {
        ioException = e;
      } finally {
        ioException = Lang.close(ioException, in);

        ioException = Lang.close(ioException, out);
      }

      Lang.rethrowIfNecessary(ioException);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy