Package org.apache.tools.zip

Examples of org.apache.tools.zip.ZipOutputStream


            String action = doUpdate ? "Updating " : "Building ";

            log(action + archiveType + ": " + zipFile.getAbsolutePath());

            ZipOutputStream zOut =
                new ZipOutputStream(new FileOutputStream(zipFile));
            zOut.setEncoding(encoding);
            try {
                if (doCompress) {
                    zOut.setMethod(ZipOutputStream.DEFLATED);
                } else {
                    zOut.setMethod(ZipOutputStream.STORED);
                }
                initZipOutputStream(zOut);

                // Add the explicit filesets to the archive.
                for (int i = 0; i < fss.length; i++) {
                    if (addThem[i].length != 0) {
                        addResources(fss[i], addThem[i], zOut);
                    }
                }
               
                if (doUpdate) {
                    addingNewFiles = false;
                    ZipFileSet oldFiles = new ZipFileSet();
                    oldFiles.setSrc(renamedFile);

                    for (int i = 0; i < addedFiles.size(); i++) {
                        PatternSet.NameEntry ne = oldFiles.createExclude();
                        ne.setName((String) addedFiles.elementAt(i));
                    }
                    DirectoryScanner ds =
                        oldFiles.getDirectoryScanner(getProject());
                    String[] f = ds.getIncludedFiles();
                    Resource[] r = new Resource[f.length];
                    for (int i = 0; i < f.length; i++) {
                        r[i] = ds.getResource(f[i]);
                    }
                   
                    addResources(oldFiles, r, zOut);
                }
                finalizeZipOutputStream(zOut);

                // If we've been successful on an update, delete the
                // temporary file
                if (doUpdate) {
                    if (!renamedFile.delete()) {
                        log ("Warning: unable to delete temporary file " +
                             renamedFile.getName(), Project.MSG_WARN);
                    }
                }
                success = true;
            } finally {
                // Close the output stream.
                try {
                    if (zOut != null) {
                        zOut.close();
                    }
                } catch (IOException ex) {
                    // If we're in this finally clause because of an
                    // exception, we don't really care if there's an
                    // exception when closing the stream. E.g. if it
View Full Code Here


            String action = doUpdate ? "Updating " : "Building ";

            log(action + archiveType + ": " + zipFile.getAbsolutePath());

            ZipOutputStream zOut = null;
            try {

                if (!skipWriting) {
                    zOut = new ZipOutputStream(zipFile);

                    zOut.setEncoding(encoding);
                    if (doCompress) {
                        zOut.setMethod(ZipOutputStream.DEFLATED);
                    } else {
                        zOut.setMethod(ZipOutputStream.STORED);
                    }
                }
                initZipOutputStream(zOut);

                // Add the explicit filesets to the archive.
                for (int i = 0; i < fss.length; i++) {
                    if (addThem[i].length != 0) {
                        addResources(fss[i], addThem[i], zOut);
                    }
                }

                if (doUpdate) {
                    addingNewFiles = false;
                    ZipFileSet oldFiles = new ZipFileSet();
                    oldFiles.setProject(getProject());
                    oldFiles.setSrc(renamedFile);

                    for (int i = 0; i < addedFiles.size(); i++) {
                        PatternSet.NameEntry ne = oldFiles.createExclude();
                        ne.setName((String) addedFiles.elementAt(i));
                    }
                    DirectoryScanner ds =
                        oldFiles.getDirectoryScanner(getProject());
                    ((ZipScanner) ds).setEncoding(encoding);

                    String[] f = ds.getIncludedFiles();
                    Resource[] r = new Resource[f.length];
                    for (int i = 0; i < f.length; i++) {
                        r[i] = ds.getResource(f[i]);
                    }

                    if (!doFilesonly) {
                        String[] d = ds.getIncludedDirectories();
                        Resource[] dr = new Resource[d.length];
                        for (int i = 0; i < d.length; i++) {
                            dr[i] = ds.getResource(d[i]);
                        }
                        Resource[] tmp = r;
                        r = new Resource[tmp.length + dr.length];
                        System.arraycopy(dr, 0, r, 0, dr.length);
                        System.arraycopy(tmp, 0, r, dr.length, tmp.length);
                    }                   
                    addResources(oldFiles, r, zOut);
                }
                finalizeZipOutputStream(zOut);

                // If we've been successful on an update, delete the
                // temporary file
                if (doUpdate) {
                    if (!renamedFile.delete()) {
                        log ("Warning: unable to delete temporary file "
                            + renamedFile.getName(), Project.MSG_WARN);
                    }
                }
                success = true;
            } finally {
                // Close the output stream.
                try {
                    if (zOut != null) {
                        zOut.close();
                    }
                } catch (IOException ex) {
                    // If we're in this finally clause because of an
                    // exception, we don't really care if there's an
                    // exception when closing the stream. E.g. if it
View Full Code Here

    protected boolean createEmptyZip(File zipFile) throws BuildException {
        if (!createEmpty) {
            return true;
        }

        ZipOutputStream zOut = null;
        try {
            log("Building MANIFEST-only jar: "
                + getDestFile().getAbsolutePath());
            zOut = new ZipOutputStream(new FileOutputStream(getDestFile()));

            zOut.setEncoding(getEncoding());
            if (isCompress()) {
                zOut.setMethod(ZipOutputStream.DEFLATED);
            } else {
                zOut.setMethod(ZipOutputStream.STORED);
            }
            initZipOutputStream(zOut);
            finalizeZipOutputStream(zOut);
        } catch (IOException ioe) {
            throw new BuildException("Could not create almost empty JAR archive"
                                     + " (" + ioe.getMessage() + ")", ioe,
                                     getLocation());
        } finally {
            // Close the output stream.
            try {
                if (zOut != null) {
                    zOut.close();
                }
            } catch (IOException ex) {
            }
            createEmpty = false;
        }
View Full Code Here

      return;
    }
    String dirPath = fileDir.getAbsolutePath();
    File descFile = new File(descFileName);
    try {
      ZipOutputStream zouts = new ZipOutputStream(new FileOutputStream(
          descFile));
      if ("*".equals(fileName) || "".equals(fileName)) {
        FileUtils.zipDirectoryToZipFile(dirPath, fileDir, zouts);
      } else {
        File file = new File(fileDir, fileName);
        if (file.isFile()) {
          FileUtils.zipFilesToZipFile(dirPath, file, zouts);
        } else {
          FileUtils
              .zipDirectoryToZipFile(dirPath, file, zouts);
        }
      }
      zouts.close();
      log.debug(descFileName + " 文件压缩成功!");
    } catch (Exception e) {
      log.debug("文件压缩失败:" + e.getMessage());
      e.printStackTrace();
    }
View Full Code Here

        this.compressor = compressor;
        this.documentationRegistry = documentationRegistry;
    }

    public WorkResult execute(final CopyActionProcessingStream stream) {
        final ZipOutputStream zipOutStr;

        try {
            zipOutStr = compressor.createArchiveOutputStream(zipFile);
        } catch (Exception e) {
            throw new GradleException(String.format("Could not create ZIP '%s'.", zipFile), e);
View Full Code Here

        zip64Mode = allowZip64Mode ? Zip64Mode.AsNeeded : Zip64Mode.Never;
    }

    public ZipOutputStream createArchiveOutputStream(File destination) {
        try {
            ZipOutputStream outStream = new ZipOutputStream(destination);
            outStream.setUseZip64(zip64Mode);
            outStream.setMethod(entryCompressionMethod);
            return outStream;
        } catch (Exception e) {
            String message = String.format("Unable to create ZIP output stream for file %s.", destination);
            throw new UncheckedIOException(message, e);
        }
View Full Code Here

        mergeFile.delete();
      }
      boolean success = false;
      List<String> skip = new ArrayList<String>();
      is = null;
      ZipOutputStream os = null;
      try {
        is = new ZipInputStream(new BufferedInputStream(new FileInputStream(destFile)));
        os = new ZipOutputStream(mergeFile);
        ZipEntry entry = null;
        while ((entry = is.getNextEntry()) != null) {
          String name = entry.getName();
          if (skip.contains(name)) {
            // already wrote merged service definition
            continue;
          }
          os.putNextEntry(new org.apache.tools.zip.ZipEntry(name));           
          if (merged.containsKey(name)) {
            // write merged service definition
            String def = merged.get(name).toString();
            // strip blank lines
            def = def.replace("\r\n", "\n").replace("\n\n", "\n");
            os.write(def.getBytes("UTF-8"));
            skip.add(name);
          } else {
            // pass-through
            int len = 0;
            byte [] buffer = new byte[4096];
            while ((len = is.read(buffer, 0, buffer.length)) != -1) {
                        os.write(buffer, 0, len);
                    }             
          }
        }
        success = true;
      } catch (Exception e) {
        console.error(e);
      } finally {
        // close input file
        if (is != null) {
          try {
            is.close();
          } catch (Exception e) {
          }
        }
        // close merged output file
        if (os != null) {
          try {
            os.flush();
            os.close();
          } catch (Exception e) {
          }
        }
       
        // successfully wrote new jar with merged META-INF/services
View Full Code Here

final class ZipArchiver extends Archiver {
    private final byte[] buf = new byte[8192];
    private final ZipOutputStream zip;

    ZipArchiver(OutputStream out) {
        zip = new ZipOutputStream(out);
        zip.setEncoding(System.getProperty("file.encoding"));
    }
View Full Code Here

TOP

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

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.