Exemplo n.º 1
0
  protected void execute() throws ArchiverException {
    getLogger().info("Expanding: " + getSourceFile() + " into " + getDestDirectory());
    ZipFile zf = null;
    try {
      zf = new ZipFile(getSourceFile(), encoding);
      Enumeration e = zf.getEntries();
      while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();
        extractFileIfIncluded(
            getSourceFile(),
            getDestDirectory(),
            zf.getInputStream(ze),
            ze.getName(),
            new Date(ze.getTime()),
            ze.isDirectory(),
            null);
      }

      getLogger().debug("expand complete");
    } catch (IOException ioe) {
      throw new ArchiverException(
          "Error while expanding " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
      if (zf != null) {
        try {
          zf.close();
        } catch (IOException e) {
          // ignore
        }
      }
    }
  }
  /**
   * Begin writing next entry.
   *
   * @since 1.1
   */
  public void putNextEntry(ZipEntry ze) throws IOException {
    closeEntry();

    entry = ze;
    entries.addElement(entry);

    if (entry.getMethod() == -1) { // not specified
      entry.setMethod(method);
    }

    if (entry.getTime() == -1) { // not specified
      entry.setTime(System.currentTimeMillis());
    }

    // Size/CRC not required if RandomAccessFile is used
    if (entry.getMethod() == STORED && raf == null) {
      // these exceptions should never happen for us (as we always write to a file)
      // so they are not localized
      if (entry.getSize() == -1) {
        throw new RuntimeException(
            "uncompressed size is required for" + " STORED method when not writing to a" + " file");
      }
      if (entry.getCrc() == -1) {
        throw new RuntimeException(
            "crc checksum is required for STORED" + " method when not writing to a file");
      }
      entry.setComprSize(entry.getSize());
    }

    if (entry.getMethod() == DEFLATED && hasCompressionLevelChanged) {
      def.setLevel(level);
      hasCompressionLevelChanged = false;
    }
    writeLocalFileHeader(entry);
  }
Exemplo n.º 3
0
  protected void execute(String path, File outputDirectory) throws ArchiverException {
    ZipFile zipFile = null;

    try {
      zipFile = new ZipFile(getSourceFile(), encoding);

      Enumeration e = zipFile.getEntries();

      while (e.hasMoreElements()) {
        ZipEntry ze = (ZipEntry) e.nextElement();

        if (ze.getName().startsWith(path)) {
          extractFileIfIncluded(
              getSourceFile(),
              outputDirectory,
              zipFile.getInputStream(ze),
              ze.getName(),
              new Date(ze.getTime()),
              ze.isDirectory(),
              null);
        }
      }
    } catch (IOException ioe) {
      throw new ArchiverException(
          "Error while expanding " + getSourceFile().getAbsolutePath(), ioe);
    } finally {
      if (zipFile != null) {
        try {
          zipFile.close();
        } catch (IOException e) {
          // ignore
        }
      }
    }
  }
Exemplo n.º 4
0
 /**
  * 解压缩功能. 将zipFilePath解压到saveDir目录
  *
  * @throws Exception
  */
 public static void upZipFile(String zipFilePath, String saveDir) throws Exception {
   ZipFile zfile = null;
   try {
     zfile = new ZipFile(zipFilePath);
     Enumeration zList = zfile.getEntries();
     ZipEntry ze = null;
     byte[] buf = new byte[1024];
     while (zList.hasMoreElements()) {
       ze = (ZipEntry) zList.nextElement();
       if (ze.isDirectory()) {
         File f = new File(saveDir + File.separatorChar + ze.getName());
         f.mkdir();
         continue;
       }
       OutputStream os = null;
       InputStream is = null;
       try {
         os =
             new BufferedOutputStream(
                 new FileOutputStream(
                     getRealFileName(saveDir + File.separatorChar, ze.getName())));
         is = new BufferedInputStream(zfile.getInputStream(ze));
         int readLen = 0;
         while ((readLen = is.read(buf, 0, 1024)) != -1) {
           os.write(buf, 0, readLen);
         }
       } finally {
         is.close();
         os.close();
       }
     }
   } finally {
     zfile.close();
   }
 }
 /**
  * Writes the data descriptor entry
  *
  * @since 1.1
  */
 protected void writeDataDescriptor(ZipEntry ze) throws IOException {
   if (ze.getMethod() != DEFLATED || raf != null) {
     return;
   }
   writeOut(DD_SIG.getBytes());
   writeOut((new ZipLong(entry.getCrc())).getBytes());
   writeOut((new ZipLong(entry.getCompressedSize())).getBytes());
   writeOut((new ZipLong(entry.getSize())).getBytes());
   written += 16;
 }
Exemplo n.º 6
0
 /** Returns a copy of this entry. */
 public Object clone() {
   try {
     ZipEntry e = (ZipEntry) super.clone();
     e.extra = (extra == null) ? null : extra.clone();
     return e;
   } catch (CloneNotSupportedException e) {
     // This should never happen, since we are Cloneable
     throw new InternalError();
   }
 }
Exemplo n.º 7
0
  /**
   * If the entry has Unicode*ExtraFields and the CRCs of the names/comments match those of the
   * extra fields, transfer the known Unicode values from the extra field.
   */
  static void setNameAndCommentFromExtraFields(
      ZipEntry ze, byte[] originalNameBytes, byte[] commentBytes) {
    UnicodePathExtraField name =
        (UnicodePathExtraField) ze.getExtraField(UnicodePathExtraField.UPATH_ID);
    String originalName = ze.getName();
    String newName = getUnicodeStringIfOriginalMatches(name, originalNameBytes);
    if (newName != null && !originalName.equals(newName)) {
      ze.setName(newName);
    }

    if (commentBytes != null && commentBytes.length > 0) {
      UnicodeCommentExtraField cmt =
          (UnicodeCommentExtraField) ze.getExtraField(UnicodeCommentExtraField.UCOM_ID);
      String newComment = getUnicodeStringIfOriginalMatches(cmt, commentBytes);
      if (newComment != null) {
        ze.setComment(newComment);
      }
    }
  }
Exemplo n.º 8
0
 /**
  * zip压缩功能. 压缩baseDir(文件夹目录)下所有文件,包括子目录
  *
  * @throws Exception
  */
 public static void zipFile(String baseDir, String fileName, String comment) throws Exception {
   List fileList = getSubFiles(new File(baseDir));
   ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(fileName));
   ZipEntry ze = null;
   byte[] buf = new byte[BUFFER];
   int readLen = 0;
   for (int i = 0; i < fileList.size(); i++) {
     File f = (File) fileList.get(i);
     ze = new ZipEntry(getAbsFileName(baseDir, f));
     ze.setSize(f.length());
     ze.setTime(f.lastModified());
     zos.putNextEntry(ze);
     InputStream is = new BufferedInputStream(new FileInputStream(f));
     while ((readLen = is.read(buf, 0, BUFFER)) != -1) {
       zos.write(buf, 0, readLen);
     }
     is.close();
   }
   zos.setComment(comment);
   zos.close();
 }
 /** Writes bytes to ZIP entry. */
 public void write(byte[] b, int offset, int length) throws IOException {
   if (entry.getMethod() == DEFLATED) {
     if (length > 0) {
       if (!def.finished()) {
         def.setInput(b, offset, length);
         while (!def.needsInput()) {
           deflate();
         }
       }
     }
   } else {
     writeOut(b, offset, length);
     written += length;
   }
   crc.update(b, offset, length);
 }
Exemplo n.º 10
0
  /**
   * Writes the central file header entry
   *
   * @since 1.1
   */
  protected void writeCentralFileHeader(ZipEntry ze) throws IOException {
    writeOut(CFH_SIG.getBytes());
    written += 4;

    // version made by
    writeOut((new ZipShort((ze.getPlatform() << 8) | 20)).getBytes());
    written += 2;

    // version needed to extract
    // general purpose bit flag
    if (ze.getMethod() == DEFLATED && raf == null) {
      // requires version 2 as we are going to store length info
      // in the data descriptor
      writeOut((new ZipShort(20)).getBytes());

      // bit3 set to signal, we use a data descriptor
      writeOut((new ZipShort(8)).getBytes());
    } else {
      writeOut((new ZipShort(10)).getBytes());
      writeOut(ZERO);
    }
    written += 4;

    // compression method
    writeOut((new ZipShort(ze.getMethod())).getBytes());
    written += 2;

    // last mod. time and date
    writeOut(toDosTime(ze.getTime()).getBytes());
    written += 4;

    // CRC
    // compressed length
    // uncompressed length
    writeOut((new ZipLong(ze.getCrc())).getBytes());
    writeOut((new ZipLong(ze.getCompressedSize())).getBytes());
    writeOut((new ZipLong(ze.getSize())).getBytes());
    written += 12;

    // file name length
    byte[] name = getBytes(ze.getName());
    writeOut((new ZipShort(name.length)).getBytes());
    written += 2;

    // extra field length
    byte[] extra = ze.getCentralDirectoryExtra();
    writeOut((new ZipShort(extra.length)).getBytes());
    written += 2;

    // file comment length
    String comm = ze.getComment();
    if (comm == null) {
      comm = "";
    }
    byte[] comment = getBytes(comm);
    writeOut((new ZipShort(comment.length)).getBytes());
    written += 2;

    // disk number start
    writeOut(ZERO);
    written += 2;

    // internal file attributes
    writeOut((new ZipShort(ze.getInternalAttributes())).getBytes());
    written += 2;

    // external file attributes
    writeOut((new ZipLong(ze.getExternalAttributes())).getBytes());
    written += 4;

    // relative offset of LFH
    writeOut(offsets.get(ze).getBytes());
    written += 4;

    // file name
    writeOut(name);
    written += name.length;

    // extra field
    writeOut(extra);
    written += extra.length;

    // file comment
    writeOut(comment);
    written += comment.length;
  }
Exemplo n.º 11
0
  /**
   * Writes the local file header entry
   *
   * @since 1.1
   */
  protected void writeLocalFileHeader(ZipEntry ze) throws IOException {
    offsets.put(ze, new ZipLong(written));

    writeOut(LFH_SIG.getBytes());
    written += 4;

    // version needed to extract
    // general purpose bit flag
    if (ze.getMethod() == DEFLATED && raf == null) {
      // requires version 2 as we are going to store length info
      // in the data descriptor
      writeOut((new ZipShort(20)).getBytes());

      // bit3 set to signal, we use a data descriptor
      writeOut((new ZipShort(8)).getBytes());
    } else {
      writeOut((new ZipShort(10)).getBytes());
      writeOut(ZERO);
    }
    written += 4;

    // compression method
    writeOut((new ZipShort(ze.getMethod())).getBytes());
    written += 2;

    // last mod. time and date
    writeOut(toDosTime(ze.getTime()).getBytes());
    written += 4;

    // CRC
    // compressed length
    // uncompressed length
    localDataStart = written;
    if (ze.getMethod() == DEFLATED || raf != null) {
      writeOut(LZERO);
      writeOut(LZERO);
      writeOut(LZERO);
    } else {
      writeOut((new ZipLong(ze.getCrc())).getBytes());
      writeOut((new ZipLong(ze.getSize())).getBytes());
      writeOut((new ZipLong(ze.getSize())).getBytes());
    }
    written += 12;

    // file name length
    byte[] name = getBytes(ze.getName());
    writeOut((new ZipShort(name.length)).getBytes());
    written += 2;

    // extra field length
    byte[] extra = ze.getLocalFileDataExtra();
    writeOut((new ZipShort(extra.length)).getBytes());
    written += 2;

    // file name
    writeOut(name);
    written += name.length;

    // extra field
    writeOut(extra);
    written += extra.length;

    dataStart = written;
  }
Exemplo n.º 12
0
  /**
   * Writes all necessary data for this entry.
   *
   * @since 1.1
   */
  public void closeEntry() throws IOException {
    if (entry == null) {
      return;
    }

    long realCrc = crc.getValue();
    crc.reset();

    if (entry.getMethod() == DEFLATED) {
      def.finish();
      while (!def.finished()) {
        deflate();
      }

      entry.setSize(def.getTotalIn());
      entry.setComprSize(def.getTotalOut());
      entry.setCrc(realCrc);

      def.reset();

      written += entry.getCompressedSize();
    } else if (raf == null) {
      if (entry.getCrc() != realCrc) {
        throw new SwcException.BadCRC(Long.toHexString(entry.getCrc()), Long.toHexString(realCrc));
      }

      if (entry.getSize() != written - dataStart) {
        throw new SwcException.BadZipSize(
            entry.getName(), entry.getSize() + "", (written - dataStart) + "");
      }
    } else {
        /* method is STORED and we used RandomAccessFile */
      long size = written - dataStart;

      entry.setSize(size);
      entry.setComprSize(size);
      entry.setCrc(realCrc);
    }

    // If random access output, write the local file header containing
    // the correct CRC and compressed/uncompressed sizes
    if (raf != null) {
      long save = raf.getFilePointer();

      raf.seek(localDataStart);
      writeOut((new ZipLong(entry.getCrc())).getBytes());
      writeOut((new ZipLong(entry.getCompressedSize())).getBytes());
      writeOut((new ZipLong(entry.getSize())).getBytes());
      raf.seek(save);
    }

    writeDataDescriptor(entry);
    entry = null;
  }
Exemplo n.º 13
0
 /**
  * Whether this library supports the compression method used by the given entry.
  *
  * @return true if the compression method is STORED or DEFLATED
  */
 private static boolean supportsMethodOf(ZipEntry entry) {
   return entry.getMethod() == ZipEntry.STORED || entry.getMethod() == ZipEntry.DEFLATED;
 }
Exemplo n.º 14
0
 /**
  * Whether this library supports the encryption used by the given entry.
  *
  * @return true if the entry isn't encrypted at all
  */
 private static boolean supportsEncryptionOf(ZipEntry entry) {
   return !entry.getGeneralPurposeBit().usesEncryption();
 }