Пример #1
0
 private void setSizesAndOffsetFromZip64Extra(
     final ZipEntry ze, final OffsetEntry offset, final int diskStart) throws IOException {
   final Zip64ExtendedInformationExtraField z64 =
       (Zip64ExtendedInformationExtraField)
           ze.getExtraField(Zip64ExtendedInformationExtraField.HEADER_ID);
   if (z64 != null) {
     final boolean hasUncompressedSize = ze.getSize() == 4294967295L;
     final boolean hasCompressedSize = ze.getCompressedSize() == 4294967295L;
     final boolean hasRelativeHeaderOffset = offset.headerOffset == 4294967295L;
     z64.reparseCentralDirectoryData(
         hasUncompressedSize, hasCompressedSize, hasRelativeHeaderOffset, diskStart == 65535);
     if (hasUncompressedSize) {
       ze.setSize(z64.getSize().getLongValue());
     } else if (hasCompressedSize) {
       z64.setSize(new ZipEightByteInteger(ze.getSize()));
     }
     if (hasCompressedSize) {
       ze.setCompressedSize(z64.getCompressedSize().getLongValue());
     } else if (hasUncompressedSize) {
       z64.setCompressedSize(new ZipEightByteInteger(ze.getCompressedSize()));
     }
     if (hasRelativeHeaderOffset) {
       offset.headerOffset = z64.getRelativeHeaderOffset().getLongValue();
     }
   }
 }
Пример #2
0
 public InputStream getInputStream(final ZipEntry ze) throws IOException, ZipException {
   if (!(ze instanceof Entry)) {
     return null;
   }
   final OffsetEntry offsetEntry = ((Entry) ze).getOffsetEntry();
   ZipUtil.checkRequestedFeatures(ze);
   final long start = offsetEntry.dataOffset;
   final BoundedInputStream bis = new BoundedInputStream(start, ze.getCompressedSize());
   switch (ze.getMethod()) {
     case 0:
       {
         return bis;
       }
     case 8:
       {
         bis.addDummy();
         final Inflater inflater = new Inflater(true);
         return new InflaterInputStream(bis, inflater) {
           public void close() throws IOException {
             super.close();
             inflater.end();
           }
         };
       }
     default:
       {
         throw new ZipException("Found unsupported compression method " + ze.getMethod());
       }
   }
 }
Пример #3
0
  /**
   * Returns the entries of an archive.
   *
   * @param ctx query context
   * @return entries
   * @throws QueryException query exception
   */
  private Iter entries(final QueryContext ctx) throws QueryException {
    final B64 archive = (B64) checkType(checkItem(expr[0], ctx), AtomType.B64);

    final ValueBuilder vb = new ValueBuilder();
    final ArchiveIn in = ArchiveIn.get(archive.input(info), info);
    try {
      while (in.more()) {
        final ZipEntry ze = in.entry();
        if (ze.isDirectory()) continue;
        final FElem e = new FElem(Q_ENTRY, NS);
        long s = ze.getSize();
        if (s != -1) e.add(Q_SIZE, token(s));
        s = ze.getTime();
        if (s != -1) e.add(Q_LAST_MOD, new Dtm(s, info).string(info));
        s = ze.getCompressedSize();
        if (s != -1) e.add(Q_COMP_SIZE, token(s));
        e.add(ze.getName());
        vb.add(e);
      }
      return vb;
    } catch (final IOException ex) {
      Util.debug(ex);
      throw ARCH_FAIL.thrw(info, ex);
    } finally {
      in.close();
    }
  }
  // TODO - zipEntry might use extended local header
  protected void add(ZipEntry zipEntry, ZipFileEntryInputStream zipData, String password)
      throws IOException, UnsupportedEncodingException {
    AESEncrypter aesEncrypter = new AESEncrypterBC(password.getBytes("iso-8859-1"));

    ExtZipEntry entry = new ExtZipEntry(zipEntry.getName());
    entry.setMethod(zipEntry.getMethod());
    entry.setSize(zipEntry.getSize());
    entry.setCompressedSize(zipEntry.getCompressedSize() + 28);
    entry.setTime(zipEntry.getTime());
    entry.initEncryptedEntry();

    zipOS.putNextEntry(entry);
    // ZIP-file data contains: 1. salt 2. pwVerification 3. encryptedContent 4. authenticationCode
    zipOS.writeBytes(aesEncrypter.getSalt());
    zipOS.writeBytes(aesEncrypter.getPwVerification());

    byte[] data = new byte[1024];
    int read = zipData.read(data);
    while (read != -1) {
      aesEncrypter.encrypt(data, read);
      zipOS.writeBytes(data, 0, read);
      read = zipData.read(data);
    }

    byte[] finalAuthentication = aesEncrypter.getFinalAuthentication();
    if (LOG.isLoggable(Level.FINE)) {
      LOG.fine(
          "finalAuthentication="
              + Arrays.toString(finalAuthentication)
              + " at pos="
              + zipOS.getWritten());
    }

    zipOS.writeBytes(finalAuthentication);
  }
Пример #5
0
  @Test
  public void compressionCanBeSetOnAPerFileBasisAndIsHonoured() throws IOException {
    // Create some input that can be compressed.
    String packageName = getClass().getPackage().getName().replace(".", "/");
    URL sample = Resources.getResource(packageName + "/sample-bytes.properties");
    byte[] input = Resources.toByteArray(sample);

    try (CustomZipOutputStream out = ZipOutputStreams.newOutputStream(output)) {
      CustomZipEntry entry = new CustomZipEntry("default");
      // Don't set the compression level. Should be the default.
      out.putNextEntry(entry);
      out.write(input);

      entry = new CustomZipEntry("stored");
      entry.setCompressionLevel(NO_COMPRESSION);
      byte[] bytes = "stored".getBytes();
      entry.setSize(bytes.length);
      entry.setCrc(Hashing.crc32().hashBytes(bytes).padToLong());
      out.putNextEntry(entry);

      out.write(bytes);

      entry = new CustomZipEntry("best");
      entry.setCompressionLevel(BEST_COMPRESSION);
      out.putNextEntry(entry);
      out.write(input);
    }

    try (ZipInputStream in = new ZipInputStream(Files.newInputStream(output))) {
      ZipEntry entry = in.getNextEntry();
      assertEquals("default", entry.getName());
      ByteStreams.copy(in, ByteStreams.nullOutputStream());
      long defaultCompressedSize = entry.getCompressedSize();
      assertNotEquals(entry.getCompressedSize(), entry.getSize());

      entry = in.getNextEntry();
      ByteStreams.copy(in, ByteStreams.nullOutputStream());
      assertEquals("stored", entry.getName());
      assertEquals(entry.getCompressedSize(), entry.getSize());

      entry = in.getNextEntry();
      ByteStreams.copy(in, ByteStreams.nullOutputStream());
      assertEquals("best", entry.getName());
      ByteStreams.copy(in, ByteStreams.nullOutputStream());
      assertThat(entry.getCompressedSize(), lessThan(defaultCompressedSize));
    }
  }
 public PackerItem(final ZipEntry zipEntry) {
   this.isDirectory = zipEntry.isDirectory();
   this.name = zipEntry.getName();
   this.compressedSize = zipEntry.getCompressedSize();
   this.size = zipEntry.getSize();
   this.time = zipEntry.getTime();
   this.compression = Integer.MIN_VALUE;
 }
Пример #7
0
  /** initializes internal hash tables with Jar file resources. */
  private void init() {
    try {
      // extracts just sizes only.
      ZipFile zf = new ZipFile(jarFileName);
      Enumeration e = zf.entries();
      while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        if (debugOn) {
          System.out.println(dumpZipEntry(ze));
        }
        htSizes.put(ze.getName(), new Integer((int) ze.getSize()));
      }
      zf.close();

      // extract resources and put them into the hashtable.
      FileInputStream fis = new FileInputStream(jarFileName);
      BufferedInputStream bis = new BufferedInputStream(fis);
      ZipInputStream zis = new ZipInputStream(bis);
      ZipEntry ze = null;
      while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
          continue;
        }
        if (debugOn) {
          System.out.println("ze.getName()=" + ze.getName() + "," + "getSize()=" + ze.getSize());
        }
        int size = (int) ze.getSize();
        // -1 means unknown size.
        if (size == -1) {
          size = ((Integer) htSizes.get(ze.getName())).intValue();
        }
        byte[] b = new byte[(int) size];
        int rb = 0;
        int chunk = 0;
        while (((int) size - rb) > 0) {
          chunk = zis.read(b, rb, (int) size - rb);
          if (chunk == -1) {
            break;
          }
          rb += chunk;
        }
        // add to internal resource hashtable
        htJarContents.put(ze.getName(), b);
        if (debugOn) {
          System.out.println(
              ze.getName() + "  rb=" + rb + ",size=" + size + ",csize=" + ze.getCompressedSize());
        }
      }
    } catch (NullPointerException e) {
      System.out.println("done.");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
Пример #8
0
 private void checkSize(ZipEntry entry) throws IOException {
   if (partSize != null) {
     long entrySize = entry.getCompressedSize();
     if ((currentSize + entrySize) > partSize) {
       closeStream();
       constructNewStream();
     }
     currentSize += entrySize;
   }
 }
Пример #9
0
  public AesZipEntry(ZipEntry zipEntry) {
    super(zipEntry.getName());
    super.setMethod(zipEntry.getMethod());
    super.setSize(zipEntry.getSize());
    super.setCompressedSize(zipEntry.getCompressedSize() + 28);
    super.setTime(zipEntry.getTime());

    flag |= 1; // bit0 - encrypted
    // flag |= 8;  // bit3 - use data descriptor
  }
Пример #10
0
  /** Adds a new file entry to the ZIP output stream. */
  void addFile(ZipOutputStream zos, File file) throws IOException {
    String name = file.getPath();
    boolean isDir = file.isDirectory();
    if (isDir) {
      name = name.endsWith(File.separator) ? name : (name + File.separator);
    }
    name = entryName(name);

    if (name.equals("") || name.equals(".") || name.equals(zname)) {
      return;
    } else if ((name.equals(MANIFEST_DIR) || name.equals(MANIFEST_NAME)) && !Mflag) {
      if (vflag) {
        output(formatMsg("out.ignore.entry", name));
      }
      return;
    }

    long size = isDir ? 0 : file.length();

    if (vflag) {
      out.print(formatMsg("out.adding", name));
    }
    ZipEntry e = new ZipEntry(name);
    e.setTime(file.lastModified());
    if (size == 0) {
      e.setMethod(ZipEntry.STORED);
      e.setSize(0);
      e.setCrc(0);
    } else if (flag0) {
      crc32File(e, file);
    }
    zos.putNextEntry(e);
    if (!isDir) {
      copy(file, zos);
    }
    zos.closeEntry();
    /* report how much compression occurred. */
    if (vflag) {
      size = e.getSize();
      long csize = e.getCompressedSize();
      out.print(formatMsg2("out.size", String.valueOf(size), String.valueOf(csize)));
      if (e.getMethod() == ZipEntry.DEFLATED) {
        long ratio = 0;
        if (size != 0) {
          ratio = ((size - csize) * 100) / size;
        }
        output(formatMsg("out.deflated", String.valueOf(ratio)));
      } else {
        output(getMsg("out.stored"));
      }
    }
  }
Пример #11
0
 private FileInfo scanZip(FileInfo zip) {
   try {
     File zf = new File(zip.pathname);
     long arcsize = zf.length();
     // ZipFile file = new ZipFile(zf);
     ArrayList<ZipEntry> entries = engine.getArchiveItems(zip.pathname);
     ArrayList<FileInfo> items = new ArrayList<FileInfo>();
     // for ( Enumeration<?> e = file.entries(); e.hasMoreElements(); ) {
     for (ZipEntry entry : entries) {
       if (entry.isDirectory()) continue;
       String name = entry.getName();
       FileInfo item = new FileInfo();
       item.format = DocumentFormat.byExtension(name);
       if (item.format == null) continue;
       File f = new File(name);
       item.filename = f.getName();
       item.path = f.getPath();
       item.pathname = entry.getName();
       item.size = (int) entry.getSize();
       // item.createTime = entry.getTime();
       item.createTime = zf.lastModified();
       item.arcname = zip.pathname;
       item.arcsize = (int) entry.getCompressedSize();
       item.isArchive = true;
       items.add(item);
     }
     if (items.size() == 0) {
       L.i("Supported files not found in " + zip.pathname);
       return null;
     } else if (items.size() == 1) {
       // single supported file in archive
       FileInfo item = items.get(0);
       item.isArchive = true;
       item.isDirectory = false;
       return item;
     } else {
       zip.isArchive = true;
       zip.isDirectory = true;
       zip.isListed = true;
       for (FileInfo item : items) {
         item.parent = zip;
         zip.addFile(item);
       }
       return zip;
     }
   } catch (Exception e) {
     L.e("IOException while opening " + zip.pathname + " " + e.getMessage());
   }
   return null;
 }
Пример #12
0
  protected void writeFile(
      boolean isDirectory, InputStream inputFile, String filename, boolean verbose)
      throws IOException {
    if (writtenItems.contains(filename)) {
      if (verbose) {
        String msg =
            MessageFormat.format(
                Messages.getString("Creator.Ignoring"), // $NON-NLS-1$
                new Object[] {filename});
        System.err.println(msg);
      }
      return;
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CRC32 crc = new CRC32();
    long size;
    if (isDirectory) {
      size = 0;
    } else {
      size = copyFile(crc, inputFile, out);
    }

    ZipEntry entry = new ZipEntry(filename);
    entry.setCrc(crc.getValue());
    entry.setSize(size);

    outputStream.putNextEntry(entry);
    out.writeTo(outputStream);
    outputStream.closeEntry();
    writtenItems.add(filename);

    if (verbose) {
      long csize = entry.getCompressedSize();
      long perc;
      if (size == 0) perc = 0;
      else perc = 100 - (100 * csize) / size;
      String msg =
          MessageFormat.format(
              Messages.getString("Creator.Adding"), // $NON-NLS-1$
              new Object[] {
                filename, Long.valueOf(size), Long.valueOf(entry.getSize()), Long.valueOf(perc)
              });
      System.err.println(msg);
    }
  }
Пример #13
0
 /**
  * Dumps a zip entry into a string.
  *
  * @param ze a ZipEntry
  */
 private String dumpZipEntry(ZipEntry ze) {
   StringBuffer sb = new StringBuffer();
   if (ze.isDirectory()) {
     sb.append("d ");
   } else {
     sb.append("f ");
   }
   if (ze.getMethod() == ZipEntry.STORED) {
     sb.append("stored   ");
   } else {
     sb.append("defalted ");
   }
   sb.append(ze.getName());
   sb.append("\t");
   sb.append("" + ze.getSize());
   if (ze.getMethod() == ZipEntry.DEFLATED) {
     sb.append("/" + ze.getCompressedSize());
   }
   return (sb.toString());
 }
Пример #14
0
  public boolean extractThemAll(String filePath) {

    File selectedFile = new File(filePath);

    if (selectedFile.isDirectory()) {

      for (File child : selectedFile.listFiles()) {
        extractThemAll(child.toString());
      }

    } else if (filePath.endsWith(".txt.gz")) {

      /**
       * Passes filepPath as the source, and filePath without the .gz as the destination file to
       * extract to.
       */
      unGZipThemAll(filePath, filePath.substring(0, filePath.lastIndexOf(".")));

    } else if (filePath.endsWith(".zip")) {

      String destDir = filePath;
      destDir = destDir.substring(0, destDir.lastIndexOf("."));
      logger("destDir is: " + destDir);

      File destDirectory = new File(destDir);

      try {

        ZipFile zipFile = new ZipFile(selectedFile);
        Enumeration<?> enu = zipFile.entries();

        while (enu.hasMoreElements()) {
          ZipEntry zipEntry = (ZipEntry) enu.nextElement();

          // avoid extracting unnecessary XML files
          if (!zipEntry.toString().endsWith(".xml")) {
            String name = zipEntry.getName(); // returns full path within the ZIP Folder
            long size = zipEntry.getSize();
            long compressedSize = zipEntry.getCompressedSize();

            File extractedFile = new File(destDirectory, name);
            if (name.endsWith("/")) {
              extractedFile.mkdirs();
              continue;
            }

            unZipThemAll(zipFile, zipEntry, extractedFile);

            if (zipEntry.getName().endsWith(".gz")) {
              String efName = extractedFile.toString();
              unGZipThemAll(efName, efName.substring(0, efName.lastIndexOf(".")));
              extractedFile.delete();
            }
          } // END OF XML IF
        } // END OF ITERATION THROUGH THE ZIP CONTAINER
        zipFile.close();
      } catch (ZipException ex) {
        Logger.getLogger(GCExtractor.class.getName()).log(Level.SEVERE, null, ex);
      } catch (IOException ex) {
        Logger.getLogger(GCExtractor.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    return true;
  } // END OF UNZIPTHEMALL
Пример #15
0
 // 测试压缩信息
 private void dump(ZipEntry ze, int rb, int size) {
   if (debugOn) {
     System.out.println(
         ze.getName() + "rb=" + rb + ",size=" + size + ",csize=" + ze.getCompressedSize());
   }
 }
Пример #16
0
 /**
  * Constructs a new instance.
  *
  * @param entry The {@link ZipEntry} allowing you to access the details of the compressed zip
  *     entry.
  * @param in An <code>InputStream</code> providing <em>direct</em> access to the zip file entry.
  */
 public SafeZipEntryInputStream(ZipEntry entry, ZipInputStream in) {
   size = entry.getCompressedSize();
   this.in = in;
 }
Пример #17
0
// =============================================================================