Example usage for java.util.zip ZipOutputStream ZipOutputStream

List of usage examples for java.util.zip ZipOutputStream ZipOutputStream

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream ZipOutputStream.

Prototype

public ZipOutputStream(OutputStream out) 

Source Link

Document

Creates a new ZIP output stream.

Usage

From source file:ZipSocket.java

public OutputStream getOutputStream() throws IOException {
    if (out == null) {
        out = new ZipOutputStream(super.getOutputStream());
    }//from w w w.  j a v  a 2 s  .co m
    return out;
}

From source file:com.twinsoft.convertigo.engine.util.ZipUtils.java

public static void makeZip(String archiveFileName, String sDir, String sRelativeDir) throws Exception {
    FileOutputStream fos = new FileOutputStream(archiveFileName);
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
    int nbZipEntries = ZipUtils.putEntries(zos, sDir, sRelativeDir, Collections.<File>emptyList());
    if (nbZipEntries > 0)
        zos.close();//from   w ww  .j  a  v  a  2 s. c o  m
}

From source file:ZipSocket.java

public OutputStream getOutputStream() 
    throws IOException {
    if (out == null) {
        out = new ZipOutputStream(super.getOutputStream());
    }// ww  w .ja v  a2 s  .co  m
    return out;
}

From source file:com.yqboots.fss.util.ZipUtils.java

/**
 * Compresses the specified directory to a zip file
 *
 * @param dir the directory to compress//w  ww.  ja v a2 s .  co m
 * @return the compressed file
 * @throws IOException
 */
public static Path compress(Path dir) throws IOException {
    Assert.isTrue(Files.exists(dir), "The directory does not exist: " + dir.toAbsolutePath());
    Assert.isTrue(Files.isDirectory(dir), "Should be a directory: " + dir.toAbsolutePath());

    Path result = Paths.get(dir.toAbsolutePath() + FileType.DOT_ZIP);
    try (final ZipOutputStream out = new ZipOutputStream(
            new BufferedOutputStream(new FileOutputStream(result.toFile())))) {
        // out.setMethod(ZipOutputStream.DEFLATED);
        final byte data[] = new byte[BUFFER];
        // get a list of files from current directory
        Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs)
                    throws IOException {
                final File file = path.toFile();

                // compress to relative directory, not absolute
                final String root = StringUtils.substringAfter(file.getParent(), dir.toString());
                try (final BufferedInputStream origin = new BufferedInputStream(new FileInputStream(file),
                        BUFFER)) {
                    final ZipEntry entry = new ZipEntry(root + File.separator + path.getFileName());
                    out.putNextEntry(entry);
                    int count;
                    while ((count = origin.read(data, 0, BUFFER)) != -1) {
                        out.write(data, 0, count);
                    }
                }

                return FileVisitResult.CONTINUE;
            }
        });
    }

    return result;
}

From source file:Main.java

/**
 * Compress a folder and its contents./*from  w ww .j  a v a 2s  .c  o m*/
 * 
 * @param srcFolder path to the folder to be compressed.
 * @param destZipFile path to the final output zip file.
 * @param addBaseFolder flag to decide whether to add also the provided base folder or not.
 * @throws IOException 
 */
static public void zipFolder(String srcFolder, String destZipFile, boolean addBaseFolder) throws IOException {
    if (new File(srcFolder).isDirectory()) {

        ZipOutputStream zip = null;
        FileOutputStream fileWriter = null;
        try {
            fileWriter = new FileOutputStream(destZipFile);
            zip = new ZipOutputStream(fileWriter);
            addFolderToZip("", srcFolder, zip, addBaseFolder); //$NON-NLS-1$
        } finally {
            if (zip != null) {
                zip.flush();
                zip.close();
            }
            if (fileWriter != null)
                fileWriter.close();
        }
    } else {
        throw new IOException(srcFolder + " is not a folder.");
    }
}

From source file:com.wisemapping.util.ZipUtils.java

public static byte[] bytesToZip(@NotNull final byte[] content) throws IOException {
    ZipOutputStream zip = null;/*from  w  w w  .j a  v a  2 s  .  co  m*/
    final ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    try {
        zip = new ZipOutputStream(byteArray);
        ZipEntry zEntry = new ZipEntry("content");
        zip.putNextEntry(zEntry);
        IOUtils.write(content, zip);
        zip.closeEntry();
    } finally {
        if (zip != null) {
            zip.flush();
            zip.close();
        }
    }

    return byteArray.toByteArray();
}

From source file:com.ecofactor.qa.automation.platform.util.FileUtil.java

/**
 * Add a folder to Zip folder./*from ww  w  .  j  a  v a 2 s. c  om*/
 * @param srcFolder the src folder
 * @param destZipFile the dest zip file
 */
public static void zipFolder(final String srcFolder, final String destZipFile) {

    try {
        final FileOutputStream fileWriter = new FileOutputStream(destZipFile);
        final ZipOutputStream zip = new ZipOutputStream(fileWriter);
        addFolderToZip("", srcFolder, zip);
        zip.flush();
        zip.close();
    } catch (Exception e) {
        LOGGER.error("Error in creating zip file", e);
    }
}

From source file:com.nuvolect.securesuite.util.OmniZip.java

public static boolean zipFiles(Context ctx, Iterable<OmniFile> files, OmniFile zipOmni, int destination) {

    ZipOutputStream zos = null;//from w  w  w  .j  a v  a  2  s  .c  om
    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "ZIPPING TO: " + zipOmni.getPath());

    try {
        zos = new ZipOutputStream(zipOmni.getOutputStream());

        for (OmniFile file : files) {

            if (file.isDirectory()) {

                zipSubDirectory(ctx, "", file, zos);
            } else {

                LogUtil.log(LogUtil.LogType.OMNI_ZIP,
                        "zipping up: " + file.getPath() + " (bytes: " + file.length() + ")");

                /**
                 * Might get lucky here, can it associate the name with the copyLarge that follows
                 */
                ZipEntry ze = new ZipEntry(file.getName());
                zos.putNextEntry(ze);

                IOUtils.copyLarge(file.getFileInputStream(), zos);

                zos.flush();
            }
        }

        zos.close();
        return true;
    } catch (IOException e) {
        LogUtil.logException(LogUtil.LogType.OMNI_ZIP, e);
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

/**
 * Zips a file at a location and places the resulting zip file at the toLocation.
 * <p/>//from ww  w  .  j  av  a2s.c  o m
 * Example:
 * zipFileAtPath("downloads/myfolder", "downloads/myFolder.zip");
 * <p/>
 * http://stackoverflow.com/a/14868161
 */
public static void zipFileAtPath(String sourcePath, String toLocation) throws IOException {
    final int BUFFER = 2048;

    File sourceFile = new File(sourcePath);
    FileOutputStream fos = new FileOutputStream(toLocation);
    ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(fos));

    if (sourceFile.isDirectory()) {
        zipSubFolder(zipOut, sourceFile, sourceFile.getParent().length() + 1); // ??

    } else {
        byte data[] = new byte[BUFFER];
        FileInputStream fis = new FileInputStream(sourcePath);
        BufferedInputStream bis = new BufferedInputStream(fis, BUFFER);

        String lastPathComponent = sourcePath.substring(sourcePath.lastIndexOf("/"));

        ZipEntry zipEntry = new ZipEntry(lastPathComponent);
        zipOut.putNextEntry(zipEntry);

        int count;
        while ((count = bis.read(data, 0, BUFFER)) != -1) {
            zipOut.write(data, 0, count);
        }
    }

    zipOut.close();
}

From source file:edu.clemson.cs.nestbed.common.util.ZipUtils.java

public static byte[] zipDirectory(File directory) throws IOException {
    byte[] data;// w w  w .  jav  a 2 s.  c om

    log.debug("zipDirectory: " + directory.getName());

    ByteArrayOutputStream baos = new ByteArrayOutputStream(BUFFER_SIZE);
    ZipOutputStream zos = new ZipOutputStream(baos);
    zipDirectory(directory, directory.getName(), zos);

    data = baos.toByteArray();
    baos.close();

    return data;
}