Exemplo n.º 1
0
  /**
   * Unzips entries from {@code stream} into {@code directory}.
   *
   * @param stream source zip stream
   * @param directory target directory to which entries going to be unzipped.
   * @param buffer the buffer to use
   * @throws IOException if an I/O error occurs.
   */
  public static void unzip(final ZipInputStream stream, final File directory, final byte[] buffer)
      throws IOException {

    if (stream == null) {
      throw new NullPointerException("stream");
    }

    if (directory == null) {
      throw new NullPointerException("directory");
    }

    if (!directory.isDirectory()) {
      throw new IllegalArgumentException("directory doesn't exist");
    }

    if (buffer == null) {
      throw new NullPointerException("buffer");
    }

    if (buffer.length == 0) {
      throw new IllegalArgumentException("buffer.length(" + buffer.length + ") == 0");
    }

    ZipEntry entry;
    while ((entry = stream.getNextEntry()) != null) {
      final File file = file(directory, entry);
      if (!entry.isDirectory()) {
        ByteStreams.copy(stream, file, buffer, -1L);
      }
      stream.closeEntry();
    }
  }
Exemplo n.º 2
0
  protected static void zip(
      final StringBuilder path,
      final File file,
      final FileFilter filter,
      final ZipOutputStream stream,
      final byte[] buffer)
      throws IOException {

    if (path == null) {
      throw new NullPointerException("path");
    }

    if (file == null) {
      throw new NullPointerException("file");
    }

    if (!file.exists()) {
      throw new IllegalArgumentException("file does not exist");
    }

    if (filter == null) {
      // throw new NullPointerException("filter");
    }

    if (stream == null) {
      throw new NullPointerException("stream");
    }

    if (buffer == null) {
      throw new NullPointerException("buffer");
    }

    if (buffer.length == 0) {
      throw new IllegalArgumentException("buffer.length(" + buffer.length + ") == 0");
    }

    if (file.isDirectory()) {
      stream.putNextEntry(new ZipEntry(path + file.getName() + "/"));
      stream.closeEntry();
      final int start = path.length();
      path.append(file.getName()).append("/");
      for (File child : file.listFiles()) {
        if (filter != null && !filter.accept(child)) {
          continue;
        }
        zip(path, child, filter, stream, buffer);
      }
      path.delete(start, path.length());
    } else {
      stream.putNextEntry(new ZipEntry(path + file.getName()));
      ByteStreams.copy(file, stream, buffer, -1L);
      stream.closeEntry();
    }
  }
Exemplo n.º 3
0
  /**
   * Unzips entries from {@code zipfile} into {@code directory}.
   *
   * @param zipfile source zip file
   * @param directory target directory to which entries going to be unzipped
   * @param buffer
   * @throws IOException if an I/O error occurs.
   */
  public static void unzip(final ZipFile zipfile, final File directory, final byte[] buffer)
      throws IOException {

    if (zipfile == null) {
      throw new NullPointerException("zipfile");
    }

    if (directory == null) {
      throw new NullPointerException("directory");
    }

    if (!directory.isDirectory()) {
      throw new IllegalArgumentException("directory doesn't exist");
    }

    if (buffer == null) {
      throw new NullPointerException("buffer");
    }

    if (buffer.length == 0) {
      throw new IllegalArgumentException("buffer.length(" + buffer.length + ") == 0");
    }

    final Enumeration<? extends ZipEntry> entries = zipfile.entries();
    while (entries.hasMoreElements()) {
      final ZipEntry entry = entries.nextElement();
      final File file = file(directory, entry);
      if (entry.isDirectory()) {
        continue;
      }
      final InputStream input = zipfile.getInputStream(entry);
      try {
        ByteStreams.copy(input, file, buffer, -1L);
      } finally {
        input.close();
      }
    }
  }