Example usage for java.util.zip ZipOutputStream putNextEntry

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

Introduction

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

Prototype

public void putNextEntry(ZipEntry e) throws IOException 

Source Link

Document

Begins writing a new ZIP file entry and positions the stream to the start of the entry data.

Usage

From source file:au.com.jwatmuff.eventmanager.util.ZipUtils.java

private static void addFolderToZip(File folder, ZipOutputStream zip, String baseName) throws IOException {
    File[] files = folder.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            addFolderToZip(file, zip, baseName);
        } else {/*from   w  ww  .j  av  a 2  s .c om*/
            String name = file.getAbsolutePath().substring(baseName.length());
            ZipEntry zipEntry = new ZipEntry(name);
            zip.putNextEntry(zipEntry);
            IOUtils.copy(new FileInputStream(file), zip);
            zip.closeEntry();
        }
    }
}

From source file:com.webautomation.ScreenCaptureHtmlUnitDriver.java

public static byte[] createZip(Map<String, byte[]> files) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ZipOutputStream zipfile = new ZipOutputStream(bos);
    Iterator<String> i = files.keySet().iterator();
    String fileName = null;/*from   w w w.j a  v a  2 s.com*/
    ZipEntry zipentry = null;
    while (i.hasNext()) {
        fileName = i.next();
        zipentry = new ZipEntry(fileName);
        zipfile.putNextEntry(zipentry);
        zipfile.write(files.get(fileName));
    }
    zipfile.close();
    return bos.toByteArray();
}

From source file:com.eviware.soapui.testondemand.TestOnDemandCaller.java

private static byte[] zipBytes(String filename, byte[] dataToBeZiped) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    ZipOutputStream zipedOutputStream = new ZipOutputStream(outputStream);
    ZipEntry entry = new ZipEntry(filename);
    entry.setSize(dataToBeZiped.length);
    try {//w ww .  j  a v a  2s  .  com
        zipedOutputStream.putNextEntry(entry);
        zipedOutputStream.write(dataToBeZiped);
    } finally {
        zipedOutputStream.closeEntry();
        zipedOutputStream.close();
    }
    return outputStream.toByteArray();
}

From source file:gov.nih.nci.caarray.services.external.v1_0.grid.client.GridDataApiUtils.java

private static void addToZip(TransferServiceContextReference transferRef, ZipOutputStream zostream)
        throws IOException, DataTransferException {
    TransferServiceContextClient tclient = null;
    try {//from www  . j  av a  2 s. c o  m
        tclient = new TransferServiceContextClient(transferRef.getEndpointReference());
        DataTransferDescriptor dd = tclient.getDataTransferDescriptor();
        zostream.putNextEntry(new ZipEntry(dd.getDataDescriptor().getName()));
        readFully(dd, zostream, true);
    } finally {
        if (tclient != null) {
            tclient.destroy();
        }
    }
}

From source file:com.pieframework.runtime.utils.Zipper.java

public static void zip(String zipFile, Map<String, File> flist) {
    byte[] buf = new byte[1024];
    try {/*from   w ww  . java2  s  .  c om*/
        // Create the ZIP file 
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

        // Compress the files 
        for (String url : flist.keySet()) {
            FileInputStream in = new FileInputStream(flist.get(url).getPath());
            // Add ZIP entry to output stream. Zip entry should be relative
            out.putNextEntry(new ZipEntry(url));
            // Transfer bytes from the file to the ZIP file 
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            // Complete the entry 
            out.closeEntry();
            in.close();
        }

        // Complete the ZIP file 
        out.close();
    } catch (Exception e) {
        throw new RuntimeException("Encountered errors zipping file " + zipFile, e);
    }
}

From source file:com.l2jfree.gameserver.util.DatabaseBackupManager.java

public static void makeBackup() {
    File f = new File(Config.DATAPACK_ROOT, Config.DATABASE_BACKUP_SAVE_PATH);
    if (!f.mkdirs() && !f.exists()) {
        _log.warn("Could not create folder " + f.getAbsolutePath());
        return;//  w  w  w .  java2  s.c  om
    }

    _log.info("DatabaseBackupManager: backing up `" + Config.DATABASE_BACKUP_DATABASE_NAME + "`...");

    Process run = null;
    try {
        run = Runtime.getRuntime().exec("mysqldump" + " --user=" + Config.DATABASE_LOGIN + " --password="
                + Config.DATABASE_PASSWORD
                + " --compact --complete-insert --default-character-set=utf8 --extended-insert --lock-tables --quick --skip-triggers "
                + Config.DATABASE_BACKUP_DATABASE_NAME, null, new File(Config.DATABASE_BACKUP_MYSQLDUMP_PATH));
    } catch (Exception e) {
    } finally {
        if (run == null) {
            _log.warn("Could not execute mysqldump!");
            return;
        }
    }

    try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        Date time = new Date();

        File bf = new File(f, sdf.format(time) + (Config.DATABASE_BACKUP_COMPRESSION ? ".zip" : ".sql"));
        if (!bf.createNewFile())
            throw new IOException("Cannot create backup file: " + bf.getCanonicalPath());
        InputStream input = run.getInputStream();
        OutputStream out = new FileOutputStream(bf);
        if (Config.DATABASE_BACKUP_COMPRESSION) {
            ZipOutputStream dflt = new ZipOutputStream(out);
            dflt.setMethod(ZipOutputStream.DEFLATED);
            dflt.setLevel(Deflater.BEST_COMPRESSION);
            dflt.setComment("L2JFree Schema Backup Utility\r\n\r\nBackup date: "
                    + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(time));
            dflt.putNextEntry(new ZipEntry(Config.DATABASE_BACKUP_DATABASE_NAME + ".sql"));
            out = dflt;
        }

        byte[] buf = new byte[4096];
        int written = 0;
        for (int read; (read = input.read(buf)) != -1;) {
            out.write(buf, 0, read);

            written += read;
        }
        input.close();
        out.close();

        if (written == 0) {
            bf.delete();
            BufferedReader br = new BufferedReader(new InputStreamReader(run.getErrorStream()));
            String line;
            while ((line = br.readLine()) != null)
                _log.warn("DatabaseBackupManager: " + line);
            br.close();
        } else
            _log.info("DatabaseBackupManager: Schema `" + Config.DATABASE_BACKUP_DATABASE_NAME
                    + "` backed up successfully in " + (System.currentTimeMillis() - time.getTime()) / 1000
                    + " s.");

        run.waitFor();
    } catch (Exception e) {
        _log.warn("DatabaseBackupManager: Could not make backup: ", e);
    }
}

From source file:de.uni_hildesheim.sse.ant.versionReplacement.PluginVersionReplacer.java

/**
 * Recursive method for adding contents (also sub folders) of a folder to a Zip file.
 * Code comes from:/*from w w  w.  j a v  a  2s  . co  m*/
 * <tt>http://www.java2s.com/Code/Java/File-Input-Output/
 * Makingazipfileofdirectoryincludingitssubdirectoriesrecursively.htm</tt>.
 * @param basePath The base path of the Zip folder (for creating relative folders inside the Zip archive).
 * @param directory The current directory which shall be zipped into the Zip archive.
 * @param out A Zip archive.
 * @throws IOException if an I/O error has occurred
 */
private static void addDir(String basePath, File directory, ZipOutputStream out) throws IOException {
    File[] files = directory.listFiles();

    for (int i = 0; i < files.length; i++) {
        String entryName = makeRelative(basePath, files[i]);
        if (null != entryName && !entryName.isEmpty()) {
            if (files[i].isDirectory()) {
                out.putNextEntry(new ZipEntry(entryName));
                out.closeEntry();
                addDir(basePath, files[i], out);
            } else {
                out.putNextEntry(new ZipEntry(entryName));
                FileInputStream in = new FileInputStream(files[i].getAbsolutePath());
                try {
                    IOUtils.copy(in, out);
                    in.close();
                } catch (IOException e1) {
                    IOUtils.closeQuietly(in);
                    throw e1;
                }
                out.closeEntry();
            }
        }
    }
}

From source file:Main.java

static private void addFileToZip(String path, File srcFile, ZipOutputStream zip, String destZipFile)
        throws Exception {
    if (srcFile.isDirectory()) {
        addFolderToZip(path, srcFile, zip, destZipFile);
    } else if (!srcFile.getName().equals(destZipFile)) {
        byte[] buf = new byte[1024];
        int len;/* w w w .  j  a  va2 s .c  o  m*/
        final InputStream in = new BufferedInputStream(new FileInputStream(srcFile));
        try {
            if (path.equals("/")) {
                zip.putNextEntry(new ZipEntry(srcFile.getName()));
            } else {
                zip.putNextEntry(new ZipEntry(path + "/" + srcFile.getName()));
            }
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            in.close();
        }
    }
}

From source file:Main.java

private static final void zip(File directory, File base, ZipOutputStream zos) throws IOException {
    File[] files = directory.listFiles();
    byte[] buffer = new byte[8192];
    int read = 0;
    for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory()) {
            zip(files[i], base, zos);/*  www  . ja v  a2s .c o m*/
        } else {
            FileInputStream in = new FileInputStream(files[i]);
            ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
            zos.putNextEntry(entry);
            while (-1 != (read = in.read(buffer))) {
                zos.write(buffer, 0, read);
            }
            in.close();
        }
    }
}

From source file:de.blizzy.backup.Utils.java

public static void zipFile(File source, File target) throws IOException {
    ZipOutputStream out = null;
    try {//from www .j  a v a2  s .co m
        out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(target)));
        out.setLevel(Deflater.BEST_COMPRESSION);

        ZipEntry entry = new ZipEntry(source.getName());
        entry.setTime(source.lastModified());
        out.putNextEntry(entry);

        Files.copy(source.toPath(), out);
    } finally {
        IOUtils.closeQuietly(out);
    }
}