示例#1
0
  private void storeFile(File file) throws FileNotFoundException, IOException {
    Console.printStatus("Storing: " + file);
    try (FileInputStream inputStream = new FileInputStream(file)) {
      _zipStream.putNextEntry(new ZipEntry(file.getCanonicalPath()));

      // Copy the content to the zip stream:

      int length;
      while ((length = inputStream.read(buffer)) > 0) _zipStream.write(buffer, 0, length);

      _zipStream.closeEntry();
    }
  }
  /**
   * Zips byte array.
   *
   * @param input Input bytes.
   * @param initBufSize Initial buffer size.
   * @return Zipped byte array.
   * @throws IOException If failed.
   */
  public static byte[] zipBytes(byte[] input, int initBufSize) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(initBufSize);

    try (ZipOutputStream zos = new ZipOutputStream(bos)) {
      ZipEntry entry = new ZipEntry("");

      try {
        entry.setSize(input.length);

        zos.putNextEntry(entry);

        zos.write(input);
      } finally {
        zos.closeEntry();
      }
    }

    return bos.toByteArray();
  }