Examples of ZipArchiveOutputStream


Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

   * @param charsetName character set to be used
   * @param includeSrc  true if src will be included.
   * @throws IOException IOException
   */
  public static void zip(File src, OutputStream os, String charsetName, boolean includeSrc) throws IOException {
    ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
    zos.setEncoding(charsetName);
    FileInputStream fis = null;

    int length;
    ZipArchiveEntry ze;
    byte[] buf = new byte[8 * 1024];
    String name;

    Stack<File> stack = new Stack<File>();
    File root;
    if (src.isDirectory()) {
      if (includeSrc) {
        stack.push(src);
        root = src.getParentFile();
      } else {
        File[] fs = checkNotNull(src.listFiles());
        for (int i = 0; i < fs.length; i++) {
          stack.push(fs[i]);
        }

        root = src;
      }
    } else {
      stack.push(src);
      root = src.getParentFile();
    }

    while (!stack.isEmpty()) {
      File f = stack.pop();
      name = toPath(root, f);
      if (f.isDirectory()) {
        File[] fs = checkNotNull(f.listFiles());
        for (int i = 0; i < fs.length; i++) {
          if (fs[i].isDirectory()) {
            stack.push(fs[i]);
          } else {
            stack.add(0, fs[i]);
          }
        }
      } else {
        ze = new ZipArchiveEntry(name);
        zos.putArchiveEntry(ze);
        try {
          fis = new FileInputStream(f);
          while ((length = fis.read(buf, 0, buf.length)) >= 0) {
            zos.write(buf, 0, length);
          }
        } finally {
          IOUtils.closeQuietly(fis);
        }
        zos.closeArchiveEntry();
      }
    }
    zos.close();
  }
View Full Code Here

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

    }

    public void testDirectoryEntryFromFile() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        ZipArchiveOutputStream zos = null;
        ZipFile zf = null;
        try {
            archive = File.createTempFile("test.", ".zip", tmp[0]);
            archive.deleteOnExit();
            zos = new ZipArchiveOutputStream(archive);
            long beforeArchiveWrite = tmp[0].lastModified();
            ZipArchiveEntry in = new ZipArchiveEntry(tmp[0], "foo");
            zos.putArchiveEntry(in);
            zos.closeArchiveEntry();
            zos.close();
            zos = null;
            zf = new ZipFile(archive);
            ZipArchiveEntry out = zf.getEntry("foo/");
            assertNotNull(out);
            assertEquals("foo/", out.getName());
            assertEquals(0, out.getSize());
            // ZIP stores time with a granularity of 2 seconds
            assertEquals(beforeArchiveWrite / 2000,
                         out.getLastModifiedDate().getTime() / 2000);
            assertTrue(out.isDirectory());
        } finally {
            ZipFile.closeQuietly(zf);
            if (zos != null) {
                zos.close();
            }
            if (archive != null) {
                archive.delete();
            }
            tmp[1].delete();
View Full Code Here

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

    }

    public void testExplicitDirectoryEntry() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        ZipArchiveOutputStream zos = null;
        ZipFile zf = null;
        try {
            archive = File.createTempFile("test.", ".zip", tmp[0]);
            archive.deleteOnExit();
            zos = new ZipArchiveOutputStream(archive);
            long beforeArchiveWrite = tmp[0].lastModified();
            ZipArchiveEntry in = new ZipArchiveEntry("foo/");
            in.setTime(beforeArchiveWrite);
            zos.putArchiveEntry(in);
            zos.closeArchiveEntry();
            zos.close();
            zos = null;
            zf = new ZipFile(archive);
            ZipArchiveEntry out = zf.getEntry("foo/");
            assertNotNull(out);
            assertEquals("foo/", out.getName());
            assertEquals(0, out.getSize());
            assertEquals(beforeArchiveWrite / 2000,
                         out.getLastModifiedDate().getTime() / 2000);
            assertTrue(out.isDirectory());
        } finally {
            ZipFile.closeQuietly(zf);
            if (zos != null) {
                zos.close();
            }
            if (archive != null) {
                archive.delete();
            }
            tmp[1].delete();
View Full Code Here

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

    }

    public void testFileEntryFromFile() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        ZipArchiveOutputStream zos = null;
        ZipFile zf = null;
        FileInputStream fis = null;
        try {
            archive = File.createTempFile("test.", ".zip", tmp[0]);
            archive.deleteOnExit();
            zos = new ZipArchiveOutputStream(archive);
            ZipArchiveEntry in = new ZipArchiveEntry(tmp[1], "foo");
            zos.putArchiveEntry(in);
            byte[] b = new byte[(int) tmp[1].length()];
            fis = new FileInputStream(tmp[1]);
            while (fis.read(b) > 0) {
                zos.write(b);
            }
            fis.close();
            fis = null;
            zos.closeArchiveEntry();
            zos.close();
            zos = null;
            zf = new ZipFile(archive);
            ZipArchiveEntry out = zf.getEntry("foo");
            assertNotNull(out);
            assertEquals("foo", out.getName());
            assertEquals(tmp[1].length(), out.getSize());
            assertEquals(tmp[1].lastModified() / 2000,
                         out.getLastModifiedDate().getTime() / 2000);
            assertFalse(out.isDirectory());
        } finally {
            ZipFile.closeQuietly(zf);
            if (zos != null) {
                zos.close();
            }
            if (archive != null) {
                archive.delete();
            }
            if (fis != null) {
View Full Code Here

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

    }

    public void testExplicitFileEntry() throws Exception {
        File[] tmp = createTempDirAndFile();
        File archive = null;
        ZipArchiveOutputStream zos = null;
        ZipFile zf = null;
        FileInputStream fis = null;
        try {
            archive = File.createTempFile("test.", ".zip", tmp[0]);
            archive.deleteOnExit();
            zos = new ZipArchiveOutputStream(archive);
            ZipArchiveEntry in = new ZipArchiveEntry("foo");
            in.setTime(tmp[1].lastModified());
            in.setSize(tmp[1].length());
            zos.putArchiveEntry(in);
            byte[] b = new byte[(int) tmp[1].length()];
            fis = new FileInputStream(tmp[1]);
            while (fis.read(b) > 0) {
                zos.write(b);
            }
            fis.close();
            fis = null;
            zos.closeArchiveEntry();
            zos.close();
            zos = null;
            zf = new ZipFile(archive);
            ZipArchiveEntry out = zf.getEntry("foo");
            assertNotNull(out);
            assertEquals("foo", out.getName());
            assertEquals(tmp[1].length(), out.getSize());
            assertEquals(tmp[1].lastModified() / 2000,
                         out.getLastModifiedDate().getTime() / 2000);
            assertFalse(out.isDirectory());
        } finally {
            ZipFile.closeQuietly(zf);
            if (zos != null) {
                zos.close();
            }
            if (archive != null) {
                archive.delete();
            }
            if (fis != null) {
View Full Code Here

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

    }

    /** Pack the content into a .wgt package */
    public void repackZip() throws IOException {
        File sourceOfFiles = new File(pathToWidget);
        ZipArchiveOutputStream out = new ZipArchiveOutputStream(new File(
            sourceOfFiles.getAbsolutePath() + File.separator + name + ".wgt"));
                try{
                    out.setEncoding("UTF-8");
            for(File aFile : sourceOfFiles.listFiles()){
            if(!aFile.getName().endsWith(".wgt")){
                pack(aFile, out, "");
            }
        }
                }
                finally{
            out.flush();
            out.close();
                }
    }
View Full Code Here

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

        if (AR.equalsIgnoreCase(archiverName)) {
            return new ArArchiveOutputStream(out);
        }
        if (ZIP.equalsIgnoreCase(archiverName)) {
            return new ZipArchiveOutputStream(out);
        }
        if (TAR.equalsIgnoreCase(archiverName)) {
            return new TarArchiveOutputStream(out);
        }
        if (JAR.equalsIgnoreCase(archiverName)) {
View Full Code Here

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

   * @param source the source file or folder to be zipped
   * @param target the zip file to create
   * @throws IOException
   */
  public static void repackZip(File source, File target) throws IOException{
    ZipArchiveOutputStream out = new ZipArchiveOutputStream(target);
    out.setEncoding("UTF-8");
        for(File afile: source.listFiles()){
            pack(afile,out, "");
        }
    out.flush();
    out.close();
  }
View Full Code Here

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

        if (AR.equalsIgnoreCase(archiverName)) {
            return new ArArchiveOutputStream(out);
        }
        if (ZIP.equalsIgnoreCase(archiverName)) {
            return new ZipArchiveOutputStream(out);
        }
        if (TAR.equalsIgnoreCase(archiverName)) {
            return new TarArchiveOutputStream(out);
        }
        if (JAR.equalsIgnoreCase(archiverName)) {
View Full Code Here

Examples of org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream

        final File targetDirectory = new File(rootDirectory, TARGET_DIRECTORY);
        final String version = project.getVersion();
        final String artifactBase = FINALNAME_PREFIX + version;
        try {
            final File targetFile = new File(distributionDirectory(targetDirectory), artifactBase + ".zip");
            final ZipArchiveOutputStream zip = new ZipArchiveOutputStream(targetFile);
            try {
                zip.setLevel(9);
                appendRecursively(sourceDirectory, artifactBase, zip, new byte[8192]);
                /*
                 * At this point, all the "application/sis-console/src/main/artifact" and sub-directories
                 * have been zipped.  Now generate the Pack200 file and zip it directly (without creating
                 * a temporary "sis.pack.gz" file).
                 */
                final Packer packer = new Packer(project.getName(), project.getUrl(), version, targetDirectory);
                final ZipArchiveEntry entry = new ZipArchiveEntry(
                        artifactBase + '/' + LIB_DIRECTORY + '/' + FATJAR_FILE + PACK_EXTENSION);
                entry.setMethod(ZipArchiveEntry.STORED);
                zip.putArchiveEntry(entry);
                packer.preparePack200(FATJAR_FILE + ".jar").pack(new FilterOutputStream(zip) {
                    /*
                     * Closes the archive entry, not the ZIP file.
                     */
                    @Override
                    public void close() throws IOException {
                        zip.closeArchiveEntry();
                    }
                });
            } finally {
                zip.close();
            }
        } catch (IOException e) {
            throw new MojoExecutionException(e.getLocalizedMessage(), e);
        }
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.