Package org.apache.tools.zip

Examples of org.apache.tools.zip.ZipFile


    if (!outDir.exists() || overWrite) {
      outDir.mkdir();
    }

    try {
      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); // 写入缓冲区内容到输出文件流
          } // while
          os.close();
          is.close();
        } // else
      } // while
      zf.close();
    } catch (IOException ios) {
      ios.printStackTrace();
      throw new IOException("在解压缩文件过程中出现未知错误");
    }
    return files;
View Full Code Here


   * @throws IOException
   */
  public static void unzip(String zipFilePath, String targetPath) throws IOException {
    OutputStream os = null;
    InputStream is = null;
    ZipFile zipFile = null;
    try {
      zipFile = new ZipFile(zipFilePath);
      String directoryPath = "";
      if (null == targetPath || "".equals(targetPath)) {
        directoryPath = zipFilePath.substring(0, zipFilePath.lastIndexOf("."));
      } 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);
            }
View Full Code Here

     * @param srcF      the source file
     * @param dir       the destination directory
     */
    protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
        log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
        ZipFile zf = null;
        FileNameMapper mapper = getMapper();
        if (!srcF.exists()) {
            throw new BuildException("Unable to expand "
                    + srcF
                    + " as the file does not exist",
                    getLocation());
        }
        try {
            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);
                }
View Full Code Here

            throw new BuildException("Source " + source.getClass().getName()
                                     + " not supported by this plugin");
        }
        InputStream inputStream = null;
        InputSource inputSource = null;
        ZipFile zf = null;

        try {
            /**
             * SAX 2 style parser used to parse the given file.
             */
            XMLReader parser = JAXPUtils.getNamespaceXMLReader();

            String uri = null;
            if (buildFile != null) {
                uri = FILE_UTILS.toURI(buildFile.getAbsolutePath());
                inputStream = new FileInputStream(buildFile);
            } else {
                uri = url.toString();
                int pling = -1;
                if (uri.startsWith("jar:file")
                    && (pling = uri.indexOf("!/")) > -1) {
                    zf = new ZipFile(org.apache.tools.ant.launch.Locator
                                     .fromJarURI(uri), "UTF-8");
                    inputStream =
                        zf.getInputStream(zf.getEntry(uri.substring(pling + 1)));
                } else {
                    inputStream = url.openStream();
                }
            }

View Full Code Here

     * @return true if the file is signed.
     * @throws IOException on error
     */
    public static boolean isSigned(File zipFile, String name)
        throws IOException {
        ZipFile jarFile = null;
        try {
            jarFile = new ZipFile(zipFile);
            if (null == name) {
                Enumeration entries = jarFile.getEntries();
                while (entries.hasMoreElements()) {
                    String eName = ((ZipEntry) entries.nextElement()).getName();
                    if (eName.startsWith(SIG_START)
                        && eName.endsWith(SIG_END)) {
                        return true;
                    }
                }
                return false;
            }
            name = replaceInvalidChars(name);
            boolean shortSig = jarFile.getEntry(SIG_START
                        + name.toUpperCase()
                        + SIG_END) != null;
            boolean longSig = false;
            if (name.length() > SHORT_SIG_LIMIT) {
                longSig = jarFile.getEntry(
                    SIG_START
                    + name.substring(0, SHORT_SIG_LIMIT).toUpperCase()
                    + SIG_END) != null;
            }

View Full Code Here

                prefix += "/";
            }
            addParentDirs(null, prefix, zOut, "", dirMode);
        }

        ZipFile zf = null;
        try {
            boolean dealingWithFiles = false;
            File base = null;

            if (zfs == null || zfs.getSrc(getProject()) == null) {
                dealingWithFiles = true;
                base = fileset.getDir(getProject());
            } else if (zfs instanceof ZipFileSet) {
                zf = new ZipFile(zfs.getSrc(getProject()), encoding);
            }

            for (int i = 0; i < resources.length; i++) {
                String name = null;
                if (fullpath.length() > 0) {
                    name = fullpath;
                } else {
                    name = resources[i].getName();
                }
                name = name.replace(File.separatorChar, '/');

                if ("".equals(name)) {
                    continue;
                }

                if (resources[i].isDirectory()) {
                    if (doFilesonly) {
                        continue;
                    }
                    int thisDirMode = zfs != null && zfs.hasDirModeBeenSet()
                        ? dirMode : getUnixMode(resources[i], zf, dirMode);
                    addDirectoryResource(resources[i], name, prefix,
                                         base, zOut,
                                         dirMode, thisDirMode);

                } else { // !isDirectory

                    addParentDirs(base, name, zOut, prefix, dirMode);

                    if (dealingWithFiles) {
                        File f = FILE_UTILS.resolveFile(base,
                                                        resources[i].getName());
                        zipFile(f, zOut, prefix + name, fileMode);
                    } else {
                        int thisFileMode =
                            zfs != null && zfs.hasFileModeBeenSet()
                            ? fileMode : getUnixMode(resources[i], zf,
                                                     fileMode);
                        addResource(resources[i], name, prefix,
                                    zOut, thisFileMode, zf,
                                    zfs == null
                                    ? null : zfs.getSrc(getProject()));
                    }
                }
            }
        } finally {
            if (zf != null) {
                zf.close();
            }
        }
    }
View Full Code Here

public class ZipExtraFieldTest extends TestCase {

    public void testPreservesExtraFields() throws IOException {
        File f = File.createTempFile("ziptest", ".zip");
        f.delete();
        ZipFile zf = null;
        try {
            Zip testInstance = new Zip();
            testInstance.setDestFile(f);
            final ZipResource r = new ZipResource() {
                    public String getName() {
                        return "x";
                    }
                    public boolean isExists() {
                        return true;
                    }
                    public boolean isDirectory() {
                        return false;
                    }
                    public long getLastModified() {
                        return 1;
                    }
                    public InputStream getInputStream() {
                        return new ByteArrayInputStream(new byte[0]);
                    }
                    public ZipExtraField[] getExtraFields() {
                        return new ZipExtraField[] {
                            new JarMarker()
                        };
                    }
                };
            testInstance.add(new ResourceCollection() {
                    public boolean isFilesystemOnly() { return false; }
                    public int size() { return 1; }
                    public Iterator iterator() {
                        ArrayList l = new ArrayList();
                        l.add(r);
                        return l.iterator();
                    }
                });
            testInstance.execute();

            zf = new ZipFile(f);
            ZipEntry ze = zf.getEntry("x");
            assertNotNull(ze);
            assertEquals(1, ze.getExtraFields().length);
            assertTrue(ze.getExtraFields()[0] instanceof JarMarker);
        } finally {
            ZipFile.closeQuietly(zf);
View Full Code Here

     */
    public InputStream getInputStream() throws IOException {
        if (isReference()) {
            return ((Resource) getCheckedRef()).getInputStream();
        }
        final ZipFile z = new ZipFile(getZipfile(), getEncoding());
        ZipEntry ze = z.getEntry(getName());
        if (ze == null) {
            z.close();
            throw new BuildException("no entry " + getName() + " in "
                                     + getArchive());
        }
        return new FilterInputStream(z.getInputStream(ze)) {
            public void close() throws IOException {
                FileUtils.close(in);
                z.close();
            }
            protected void finalize() throws Throwable {
                try {
                    close();
                } finally {
View Full Code Here

    /**
     * fetches information from the named entry inside the archive.
     */
    protected void fetchEntry() {
        ZipFile z = null;
        try {
            z = new ZipFile(getZipfile(), getEncoding());
            setEntry(z.getEntry(getName()));
        } catch (IOException e) {
            log(e.getMessage(), Project.MSG_DEBUG);
            throw new BuildException(e);
        } finally {
            ZipFile.closeQuietly(z);
View Full Code Here

     */
    protected void fillMapsFromArchive(Resource src, String encoding,
                                       Map fileEntries, Map matchFileEntries,
                                       Map dirEntries, Map matchDirEntries) {
        ZipEntry entry = null;
        ZipFile zf = null;

        File srcFile = null;
        FileProvider fp = (FileProvider) src.as(FileProvider.class);
        if (fp != null) {
            srcFile = fp.getFile();
        } else {
            throw new BuildException("Only file provider resources are supported");
        }

        try {
            try {
                zf = new ZipFile(srcFile, encoding);
            } catch (ZipException ex) {
                throw new BuildException("Problem reading " + srcFile, ex);
            } catch (IOException ex) {
                throw new BuildException("Problem opening " + srcFile, ex);
            }
            Enumeration e = zf.getEntries();
            while (e.hasMoreElements()) {
                entry = (ZipEntry) e.nextElement();
                Resource r = new ZipResource(srcFile, encoding, entry);
                String name = entry.getName();
                if (entry.isDirectory()) {
View Full Code Here

TOP

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

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.