Example usage for java.util.zip ZipOutputStream closeEntry

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

Introduction

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

Prototype

public void closeEntry() throws IOException 

Source Link

Document

Closes the current ZIP entry and positions the stream for writing the next entry.

Usage

From source file:org.apache.hadoop.yarn.util.TestFSDownload.java

static LocalResource createZipFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis)
        throws IOException, URISyntaxException {
    byte[] bytes = new byte[len];
    r.nextBytes(bytes);/*from w  w w. java2  s  . c om*/

    File archiveFile = new File(p.toUri().getPath() + ".ZIP");
    archiveFile.createNewFile();
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(archiveFile));
    out.putNextEntry(new ZipEntry(p.getName()));
    out.write(bytes);
    out.closeEntry();
    out.close();

    LocalResource ret = recordFactory.newRecordInstance(LocalResource.class);
    ret.setResource(URL.fromPath(new Path(p.toString() + ".ZIP")));
    ret.setSize(len);
    ret.setType(LocalResourceType.ARCHIVE);
    ret.setVisibility(vis);
    ret.setTimestamp(files.getFileStatus(new Path(p.toString() + ".ZIP")).getModificationTime());
    return ret;
}

From source file:net.firejack.platform.core.utils.ArchiveUtils.java

private static void zipEntry(File file, ZipOutputStream out, String delPath) throws IOException {
    if (file.isFile()) {
        FileInputStream in = new FileInputStream(file);
        out.putNextEntry(new ZipEntry(file.getPath().replace(delPath + File.separator, "")));
        IOUtils.copy(in, out);// w  w  w .  j a va  2 s  .  com

        out.closeEntry();
        IOUtils.closeQuietly(in);
    } else {
        for (File f : file.listFiles())
            zipEntry(f, out, delPath);
    }
}

From source file:net.grinder.util.LogCompressUtil.java

/**
 * Compress the given file./*from  www.ja  v a  2 s  .c  o  m*/
 * 
 * @param logFile
 *            file to be compressed
 * @return compressed file byte array
 */
public static byte[] compressFile(File logFile) {
    FileInputStream fio = null;
    ByteArrayOutputStream out = null;
    ZipOutputStream zos = null;
    try {
        fio = new FileInputStream(logFile);
        out = new ByteArrayOutputStream();
        zos = new ZipOutputStream(out);
        ZipEntry zipEntry = new ZipEntry("log.txt");
        zipEntry.setTime(new Date().getTime());
        zos.putNextEntry(zipEntry);
        byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
        int count = 0;
        while ((count = fio.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
            zos.write(buffer, 0, count);
        }
        zos.closeEntry();
        zos.finish();
        zos.flush();
        return out.toByteArray();
    } catch (IOException e) {
        LOGGER.error("Error occurs while compress {}", logFile.getAbsolutePath());
        LOGGER.error("Details", e);
        return null;
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(fio);
        IOUtils.closeQuietly(out);
    }
}

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

/**
 * Copy to zip.// www. java  2 s.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:com.pieframework.runtime.utils.Zipper.java

public static void zip(String zipFile, Map<String, File> flist) {
    byte[] buf = new byte[1024];
    try {// w w  w  . j a v a  2s  . co m
        // 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:org.apache.hadoop.hive.ql.metadata.JarUtils.java

private static void zipDir(File dir, String relativePath, ZipOutputStream zos, boolean start)
        throws IOException {
    String[] dirList = dir.list();
    for (String aDirList : dirList) {
        File f = new File(dir, aDirList);
        if (!f.isHidden()) {
            if (f.isDirectory()) {
                if (!start) {
                    ZipEntry dirEntry = new ZipEntry(relativePath + f.getName() + "/");
                    zos.putNextEntry(dirEntry);
                    zos.closeEntry();
                }/*from  w w  w . j  a  v  a 2  s .  c om*/
                String filePath = f.getPath();
                File file = new File(filePath);
                zipDir(file, relativePath + f.getName() + "/", zos, false);
            } else {
                String path = relativePath + f.getName();
                if (!path.equals(JarFile.MANIFEST_NAME)) {
                    ZipEntry anEntry = new ZipEntry(path);
                    InputStream is = new FileInputStream(f);
                    copyToZipStream(is, anEntry, zos);
                }
            }
        }
    }
}

From source file:com.chinamobile.bcbsp.fault.tools.Zip.java

/**
 * Compress files to *.zip.//from w ww.ja v a 2s.c om
 * @param fileName
 *        file name to compress
 * @return compressed file.
 */
public static String compress(String fileName) {
    String targetFile = null;
    File sourceFile = new File(fileName);
    Vector<File> vector = getAllFiles(sourceFile);
    try {
        if (sourceFile.isDirectory()) {
            targetFile = fileName + ".zip";
        } else {
            char ch = '.';
            targetFile = fileName.substring(0, fileName.lastIndexOf(ch)) + ".zip";
        }
        BufferedInputStream bis = null;
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));
        ZipOutputStream zipos = new ZipOutputStream(bos);
        byte[] data = new byte[BUFFER];
        if (vector.size() > 0) {
            for (int i = 0; i < vector.size(); i++) {
                File file = vector.get(i);
                zipos.putNextEntry(new ZipEntry(getEntryName(fileName, file)));
                bis = new BufferedInputStream(new FileInputStream(file));
                int count;
                while ((count = bis.read(data, 0, BUFFER)) != -1) {
                    zipos.write(data, 0, count);
                }
                bis.close();
                zipos.closeEntry();
            }
            zipos.close();
            bos.close();
            return targetFile;
        } else {
            File zipNullfile = new File(targetFile);
            zipNullfile.getParentFile().mkdirs();
            zipNullfile.mkdir();
            return zipNullfile.getAbsolutePath();
        }
    } catch (IOException e) {
        LOG.error("[compress]", e);
        return "error";
    }
}

From source file:org.apache.oodt.product.handlers.ofsn.util.OFSNUtils.java

public static File buildZipFile(String zipFileFullPath, File[] files) {
    // Create a buffer for reading the files
    byte[] buf = new byte[INT];
    ZipOutputStream out = null;

    try {//from   ww  w . j a v  a2 s. co  m
        // Create the ZIP file
        out = new ZipOutputStream(new FileOutputStream(zipFileFullPath));

        for (File file : files) {
            FileInputStream in = new FileInputStream(file);

            // Add ZIP entry to output stream.
            out.putNextEntry(new ZipEntry(file.getName()));

            // 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();
        }
    } catch (IOException e) {
        LOG.log(Level.SEVERE, e.getMessage());
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Exception ignore) {
            }

        }
    }

    return new File(zipFileFullPath);

}

From source file:io.druid.java.util.common.CompressionUtils.java

public static void makeEvilZip(File outputFile) throws IOException {
    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(outputFile));
    ZipEntry zipEntry = new ZipEntry("../../../../../../../../../../../../../../../tmp/evil.txt");
    zipOutputStream.putNextEntry(zipEntry);
    byte[] output = StringUtils.toUtf8("evil text");
    zipOutputStream.write(output);//  ww w  .  jav a2 s  .c  om
    zipOutputStream.closeEntry();
    zipOutputStream.close();
}

From source file:ObjectLabEnterpriseSoftware.FileManager.java

public static void zipFolder(ZipOutputStream zipOutputStream, File inputFolder, String parentName)
        throws IOException {

    String myname = parentName + inputFolder.getName() + "\\";

    ZipEntry folderZipEntry = new ZipEntry(myname);
    zipOutputStream.putNextEntry(folderZipEntry);

    File[] contents = inputFolder.listFiles();
    for (File f : contents) {
        if (f.isFile())
            zipFile(f, myname, zipOutputStream);

        else if (f.isDirectory())
            zipFolder(zipOutputStream, f, myname);
    }/*from   w w w.  j a  va 2 s  .c  o m*/
    zipOutputStream.closeEntry();
}