Package org.apache.tools.zip

Examples of org.apache.tools.zip.ZipEntry


      ZipFile zf = new ZipFile(inZip, "GBK"); // 打开压缩文件
      byte[] buffer = new byte[DECOMPRESS_BUFFER_SIZE]; // 解压缩文件时的缓存区

      Enumeration e = zf.getEntries();
      while (e.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) e.nextElement(); // 保存每次读到的entry的引用
        String path = outDir.getPath() + fileSeparator
            + entry.getName().replace('/', fileSeparator);
        files.add(entry.getName().replace('/', fileSeparator));
        if (entry.isDirectory()) { // 处理目录类型的entry
          createDir(path, false);
        } // if
        else { // 处理文件类型的entry
          createDir(path, true);
          InputStream is = zf.getInputStream(entry); // 得到文件类型entry的输入流
          FileOutputStream os = new FileOutputStream(path); // 解压后的输出文件

          long leftSize = entry.getSize(); // 保存输入流中剩下未读的字节数
          int readSize = 0; // 保存实际每次读入的字节数
          while (leftSize > 0) { // 如果输入流中剩下的字节数大于0则继续读
            readSize = is.read(buffer); // 读入到缓冲区,readSize为实际读入的字节数
            leftSize -= readSize; // 更新输入流中剩下的字节数信息
            os.write(buffer, 0, readSize); // 写入缓冲区内容到输出文件流
View Full Code Here


  private static void zipFile(String basePath, File file,
      ZipOutputStream zout, byte[] buffer) throws IOException {

    if (file.isDirectory()) { // 处理目录

      ZipEntry entry = new ZipEntry(basePath + "/");
      entry.setSize(0);
      entry.setTime(System.currentTimeMillis());
      zout.putNextEntry(entry);

      String[] subFile = file.list(); // 子文件和目录的列表
      for (int i = 0; i < subFile.length; i++) {
        zipFile(basePath + "/" + subFile[i], new File(file.getPath()
            + fileSeparator + subFile[i]), zout, buffer);
      }
    } else { // 处理文件
      FileInputStream is = new FileInputStream(file);

      ZipEntry entry = new ZipEntry(basePath);
      entry.setSize(is.available());
      entry.setTime(System.currentTimeMillis());
      zout.putNextEntry(entry);

      long leftSize = is.available(); // 保存输入流中剩下未读的字节数
      int readSize = 0; // 保存实际每次读入的字节数
      while (leftSize > 0) { // 如果输入流中剩下的字节数大于0则继续读
View Full Code Here

   * @param relativePath
   *            相对路径
   * @throws IOException
   */
  private static void zipFile(ZipOutputStream zos, File file, String relativePath) throws IOException {
    ZipEntry entry = new ZipEntry(relativePath + file.getName());
    zos.putNextEntry(entry);
    InputStream is = null;
    try {
      is = new FileInputStream(file);
      int BUFFERSIZE = 2 << 10;
View Full Code Here

   * @param relativePath
   *            相对路径
   * @throws IOException
   */
  private static void createZipNode(ZipOutputStream zos, String relativePath) throws IOException {
    ZipEntry zipEntry = new ZipEntry(relativePath);
    zos.putNextEntry(zipEntry);
    zos.closeEntry();
  }
View Full Code Here

      } else {
        directoryPath = targetPath;
      }
      Enumeration entryEnum = zipFile.getEntries();
      if (null != entryEnum) {
        ZipEntry zipEntry = null;
        while (entryEnum.hasMoreElements()) {
          zipEntry = (ZipEntry) entryEnum.nextElement();
          if (zipEntry.isDirectory()) {
            directoryPath = directoryPath + File.separator + zipEntry.getName();
            org.jeecgframework.core.util.LogUtil.info(directoryPath);
            continue;
          }
          if (zipEntry.getSize() > 0) {
            // 文件
            File targetFile = buildFile(directoryPath + File.separator + zipEntry.getName(), false);
            os = new BufferedOutputStream(new FileOutputStream(targetFile));
            is = zipFile.getInputStream(zipEntry);
            byte[] buffer = new byte[4096];
            int readLen = 0;
            while ((readLen = is.read(buffer, 0, 4096)) >= 0) {
              os.write(buffer, 0, readLen);
            }
            os.flush();
            os.close();
          } else {
            // 空目录
            buildFile(directoryPath + File.separator + zipEntry.getName(), true);
          }
        }
      }
    } catch (IOException ex) {
      throw ex;
View Full Code Here

            zf = new ZipFile(srcF, encoding, scanForUnicodeExtraFields);
            boolean empty = true;
            Enumeration e = zf.getEntries();
            while (e.hasMoreElements()) {
                empty = false;
                ZipEntry ze = (ZipEntry) e.nextElement();
                InputStream is = null;
                log("extracting " + ze.getName(), Project.MSG_DEBUG);
                try {
                    extractFile(fileUtils, srcF, dir,
                                is = zf.getInputStream(ze),
                                ze.getName(), new Date(ze.getTime()),
                                ze.isDirectory(), mapper);
                } finally {
                    FileUtils.close(is);
                }
            }
            if (empty && getFailOnEmptyArchive()) {
View Full Code Here

    private int getUnixMode(Resource r, ZipFile zf, int defaultMode)
        throws IOException {

        int unixMode = defaultMode;
        if (zf != null) {
            ZipEntry ze = zf.getEntry(r.getName());
            unixMode = ze.getUnixMode();
            if ((unixMode == 0 || unixMode == UnixStat.DIR_FLAG)
                && !preserve0Permissions) {
                unixMode = defaultMode;
            }
        } else if (r instanceof ArchiveResource) {
View Full Code Here

                             ZipOutputStream zOut, int mode,
                             ZipFile zf, File fromArchive)
        throws IOException {

        if (zf != null) {
            ZipEntry ze = zf.getEntry(r.getName());

            if (ze != null) {
                boolean oldCompress = doCompress;
                if (keepCompression) {
                    doCompress = (ze.getMethod() == ZipEntry.DEFLATED);
                }
                InputStream is = null;
                try {
                    is = zf.getInputStream(ze);
                    zipFile(is, zOut, prefix + name, ze.getTime(),
                            fromArchive, mode, ze.getExtraFields(true));
                } finally {
                    doCompress = oldCompress;
                    FileUtils.close(is);
                }
            }
View Full Code Here

        logWhenWriting("adding directory " + vPath, Project.MSG_VERBOSE);
        addedDirs.put(vPath, vPath);

        if (!skipWriting) {
            ZipEntry ze = new ZipEntry (vPath);

            // ZIPs store time with a granularity of 2 seconds, round up
            int millisToAdd = roundUp ? ROUNDUP_MILLIS : 0;

            if (dir != null && dir.isExists()) {
                ze.setTime(dir.getLastModified() + millisToAdd);
            } else {
                ze.setTime(System.currentTimeMillis() + millisToAdd);
            }
            ze.setSize (0);
            ze.setMethod (ZipEntry.STORED);
            // This is faintly ridiculous:
            ze.setCrc (EMPTY_CRC);
            ze.setUnixMode(mode);

            if (extra != null) {
                ze.setExtraFields(extra);
            }

            zOut.putNextEntry(ze);
        }
    }
View Full Code Here

        }

        entries.put(vPath, vPath);

        if (!skipWriting) {
            ZipEntry ze = new ZipEntry(vPath);
            ze.setTime(lastModified);
            ze.setMethod(doCompress ? ZipEntry.DEFLATED : ZipEntry.STORED);

            /*
             * ZipOutputStream.putNextEntry expects the ZipEntry to
             * know its size and the CRC sum before you start writing
             * the data when using STORED mode - unless it is seekable.
             *
             * This forces us to process the data twice.
             */
            if (!zOut.isSeekable() && !doCompress) {
                long size = 0;
                CRC32 cal = new CRC32();
                if (!in.markSupported()) {
                    // Store data into a byte[]
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();

                    byte[] buffer = new byte[BUFFER_SIZE];
                    int count = 0;
                    do {
                        size += count;
                        cal.update(buffer, 0, count);
                        bos.write(buffer, 0, count);
                        count = in.read(buffer, 0, buffer.length);
                    } while (count != -1);
                    in = new ByteArrayInputStream(bos.toByteArray());

                } else {
                    in.mark(Integer.MAX_VALUE);
                    byte[] buffer = new byte[BUFFER_SIZE];
                    int count = 0;
                    do {
                        size += count;
                        cal.update(buffer, 0, count);
                        count = in.read(buffer, 0, buffer.length);
                    } while (count != -1);
                    in.reset();
                }
                ze.setSize(size);
                ze.setCrc(cal.getValue());
            }

            ze.setUnixMode(mode);
            ZipExtraField[] extra = getCurrentExtraFields();
            if (extra != null) {
                ze.setExtraFields(extra);
            }

            zOut.putNextEntry(ze);

            byte[] buffer = new byte[BUFFER_SIZE];
View Full Code Here

TOP

Related Classes of org.apache.tools.zip.ZipEntry

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.