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:de.tor.tribes.util.AttackToTextWriter.java

private static boolean writeBlocksToZip(List<String> pBlocks, Tribe pTribe, File pPath) {
    int fileNo = 1;
    String baseFilename = pTribe.getName().replaceAll("\\W+", "");
    ZipOutputStream zout = null;
    try {//from  ww  w  .ja v a 2  s  .  com
        zout = new ZipOutputStream(
                new FileOutputStream(FilenameUtils.concat(pPath.getPath(), baseFilename + ".zip")));
        for (String block : pBlocks) {
            String entryName = baseFilename + fileNo + ".txt";
            ZipEntry entry = new ZipEntry(entryName);
            try {
                zout.putNextEntry(entry);
                zout.write(block.getBytes());
                zout.closeEntry();
            } catch (IOException ioe) {
                logger.error("Failed to write attack to zipfile", ioe);
                return false;
            }
            fileNo++;
        }
    } catch (IOException ioe) {
        logger.error("Failed to write content to zip file", ioe);
        return false;
    } finally {
        if (zout != null) {
            try {
                zout.flush();
                zout.close();
            } catch (IOException ignored) {
            }
        }
    }
    return true;
}

From source file:cc.recommenders.utils.Zips.java

public static void zip(File directory, File out) throws IOException {
    ZipOutputStream zos = null;
    try {/*w w w .j a  v a  2s . c  o  m*/
        OutputSupplier<FileOutputStream> s = Files.newOutputStreamSupplier(out);
        zos = new ZipOutputStream(s.getOutput());
        Collection<File> files = FileUtils.listFiles(directory, FILE, DIRECTORY);
        for (File f : files) {
            String path = removeStart(f.getPath(), directory.getAbsolutePath() + "/");
            ZipEntry e = new ZipEntry(path);
            zos.putNextEntry(e);
            byte[] data = Files.toByteArray(f);
            zos.write(data);
            zos.closeEntry();
        }
    } finally {
        Closeables.close(zos, false);
    }
}

From source file:de.micromata.genome.gdbfs.FileSystemUtils.java

/**
 * Copy to zip.// w  ww . j av a2s .c  o m
 *
 * @param source the source
 * @param zout the zout
 */
public static void copyToZip(FsObject source, ZipOutputStream zout) {
    String name = source.getName();
    if (source.isDirectory() == true) {
        name += "/";
    }
    ZipEntry ze = new ZipEntry(name);
    try {
        if (source.isFile() == true) {
            zout.putNextEntry(ze);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            source.getFileSystem().readBinaryFile(source.getName(), bout);
            zout.write(bout.toByteArray());
            zout.closeEntry();
        } else if (source.isDirectory() == true) {
            zout.putNextEntry(ze);
            zout.closeEntry();
        }
    } catch (IOException ex) {
        throw new RuntimeIOException(ex);
    }
}

From source file:org.openmrs.module.omodexport.util.OmodExportUtil.java

/**
 * Utility method for zipping a list of files
 * /*from   w  w  w .j  ava 2  s .c o  m*/
 * @param files -The list of files to zip
 * @param out
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void zipFiles(List<File> files, ZipOutputStream out) throws FileNotFoundException, IOException {
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytesRead;

    for (File f : files) {
        if (f.isDirectory())
            continue;// Ignore directory
        FileInputStream in = new FileInputStream(f); // Stream to read file
        out.putNextEntry(new ZipEntry(f.getName())); // Store entry
        while ((bytesRead = in.read(buffer)) != -1)
            out.write(buffer, 0, bytesRead);
        in.close();
    }

    out.close();
}

From source file:com.recomdata.transmart.data.export.util.ZipUtil.java

/**
 * This method will bundle all the files into a zip file. 
 * If there are 2 files with the same name, only the first file is part of the zip.
 * //  www . j a va  2s .  c o  m
 * @param zipFileName
 * @param files
 * 
 * @return zipFile absolute path
 * 
 */
public static String bundleZipFile(String zipFileName, List<File> files) {
    File zipFile = null;
    Map<String, File> filesMap = new HashMap<String, File>();

    if (StringUtils.isEmpty(zipFileName))
        return null;

    try {
        zipFile = new File(zipFileName);
        if (zipFile.exists() && zipFile.isFile() && zipFile.delete()) {
            zipFile = new File(zipFileName);
        }

        ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
        zipOut.setLevel(ZipOutputStream.DEFLATED);
        byte[] buffer = new byte[BUFFER_SIZE];

        for (File file : files) {
            if (filesMap.containsKey(file.getName())) {
                continue;
            } else if (file.exists() && file.canRead()) {
                filesMap.put(file.getName(), file);
                zipOut.putNextEntry(new ZipEntry(file.getName()));
                FileInputStream fis = new FileInputStream(file);
                int bytesRead;
                while ((bytesRead = fis.read(buffer)) != -1) {
                    zipOut.write(buffer, 0, bytesRead);
                }
                zipOut.flush();
                zipOut.closeEntry();
            }
        }
        zipOut.finish();
        zipOut.close();
    } catch (IOException e) {
        //log.error("Error while creating Zip file");
    }

    return (null != zipFile) ? zipFile.getAbsolutePath() : null;
}

From source file:de.fu_berlin.inf.dpp.netbeans.feedback.ErrorLogManager.java

/**
 * Convenience wrapper method to upload an error log file to the server. To
 * save time and storage space, the log is compressed to a zip archive with
 * the given zipName./*www  . j  a  va  2  s.  com*/
 * 
 * @param zipName
 *            a name for the zip archive, e.g. with added user ID to make it
 *            unique, zipName must be at least 3 characters long!
 * @throws IOException
 *             if an I/O error occurs
 */
private static void uploadErrorLog(String zipName, File file, IProgressMonitor monitor) throws IOException {

    if (ERROR_LOG_UPLOAD_URL == null) {
        log.warn("error log upload url is not configured, cannot upload error log file");
        return;
    }

    File archive = new File(System.getProperty("java.io.tmpdir"), zipName + ".zip");

    ZipOutputStream out = null;
    FileInputStream in = null;

    byte[] buffer = new byte[8192];

    try {

        in = new FileInputStream(file);

        out = new ZipOutputStream(new FileOutputStream(archive));

        out.putNextEntry(new ZipEntry(file.getName()));

        int read;

        while ((read = in.read(buffer)) > 0)
            out.write(buffer, 0, read);

        out.finish();
        out.close();

        FileSubmitter.uploadFile(archive, ERROR_LOG_UPLOAD_URL, monitor);
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(in);
        archive.delete();
    }
}

From source file:Compress.java

/** Zip the contents of the directory, and save it in the zipfile */
public static void zipDirectory(String dir, String zipfile) throws IOException, IllegalArgumentException {
    // Check that the directory is a directory, and get its contents
    File d = new File(dir);
    if (!d.isDirectory())
        throw new IllegalArgumentException("Compress: not a directory:  " + dir);
    String[] entries = d.list();/*from  www  .j  av a 2  s  .  com*/
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytes_read;

    // Create a stream to compress data and write it to the zipfile
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));

    // Loop through all entries in the directory
    for (int i = 0; i < entries.length; i++) {
        File f = new File(d, entries[i]);
        if (f.isDirectory())
            continue; // Don't zip sub-directories
        FileInputStream in = new FileInputStream(f); // Stream to read file
        ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
        out.putNextEntry(entry); // Store entry
        while ((bytes_read = in.read(buffer)) != -1)
            // Copy bytes
            out.write(buffer, 0, bytes_read);
        in.close(); // Close input stream
    }
    // When we're done with the whole loop, close the output stream
    out.close();
}

From source file:Main.java

static void zipFile(File zipfile, ZipOutputStream zos, String name) throws IOException {
    // if we reached here, the File object f was not a directory 
    // create a FileInputStream on top of f

    FileInputStream fis = new FileInputStream(zipfile);
    try {/*from   w  w w .  j  a  v a2  s. c  o m*/
        // create a new zip entry 
        ZipEntry anEntry = new ZipEntry(name);
        if (DEBUG)
            System.out.println("Add file : " + name);
        // place the zip entry in the ZipOutputStream object
        zos.putNextEntry(anEntry);
        // now write the content of the file to the
        // ZipOutputStream
        byte[] readBuffer = new byte[BUFFER_SIZE];
        for (int bytesIn = fis.read(readBuffer); bytesIn != -1; bytesIn = fis.read(readBuffer)) {
            zos.write(readBuffer, 0, bytesIn);
        }
    } finally {
        // close the Stream
        fis.close();
    }
}

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

private static void zipSubDirectory(Context ctx, String basePath, OmniFile dir, ZipOutputStream zos)
        throws IOException {

    LogUtil.log(LogUtil.LogType.OMNI_ZIP, "zipSubDirectory : " + dir.getPath());

    OmniFile[] files = dir.listFiles();//from  ww w. j  a  v a  2s . co  m

    for (OmniFile file : files) {

        if (file.isDirectory()) {
            String path = basePath + file.getName() + File.separator;
            zos.putNextEntry(new ZipEntry(path));
            zipSubDirectory(ctx, path, file, zos);
            zos.closeEntry();
        } else {
            if (file.isStd())// don't scan crypto volume
                MediaScannerConnection.scanFile(ctx, new String[] { file.getAbsolutePath() }, null, null);

            zipFile(basePath, file, zos);
        }
    }
}

From source file:es.sm2.openppm.core.plugin.action.GenericAction.java

/**
 *
 * @param files/*from w w w  .  ja  va 2 s.c o  m*/
 * @return
 * @throws IOException
 */
public static byte[] zipFiles(List<File> files) throws IOException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(baos);

    for (File f : files) {

        zos.putNextEntry(new ZipEntry(f.getName()));

        zos.write(getBytesFromFile(f.getAbsoluteFile()));

        zos.closeEntry();
    }

    zos.flush();
    baos.flush();
    zos.close();
    baos.close();

    return baos.toByteArray();
}