Example usage for java.util.zip ZipEntry ZipEntry

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

Introduction

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

Prototype

public ZipEntry(ZipEntry e) 

Source Link

Document

Creates a new zip entry with fields taken from the specified zip entry.

Usage

From source file:Main.java

public static String compress(String filename) throws FileNotFoundException, IOException {
    byte[] buffer = new byte[4096];
    int bytesRead;
    String[] entries = { ".mp4" };
    String zipfile = filename.replace(".mp4", ".zip");
    if (!(new File(zipfile)).exists()) {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
        out.setLevel(Deflater.BEST_COMPRESSION);
        out.setMethod(ZipOutputStream.DEFLATED);
        for (int i = 0; i < entries.length; i++) {
            File f = new File(filename.replace(".mp4", entries[i]));
            if (f.exists()) {
                FileInputStream in = new FileInputStream(f);
                ZipEntry entry = new ZipEntry(f.getName());
                out.putNextEntry(entry);
                while ((bytesRead = in.read(buffer)) != -1)
                    out.write(buffer, 0, bytesRead);
                in.close();//ww w. j  a v a  2  s  .c  o  m
            }
        }
        out.close();
    }
    return zipfile;
}

From source file:Main.java

private static void zipFile(ZipOutputStream zipOutputStream, String sourcePath) throws IOException {

    java.io.File files = new java.io.File(sourcePath);
    java.io.File[] fileList = files.listFiles();

    String entryPath = "";
    BufferedInputStream input = null;
    for (java.io.File file : fileList) {
        if (file.isDirectory()) {
            zipFile(zipOutputStream, file.getPath());
        } else {/*from  www . j  av  a2 s.co m*/
            byte data[] = new byte[BUFFER_SIZE];
            FileInputStream fileInputStream = new FileInputStream(file.getPath());
            input = new BufferedInputStream(fileInputStream, BUFFER_SIZE);
            entryPath = file.getAbsolutePath().replace(parentPath, "");

            ZipEntry entry = new ZipEntry(entryPath);
            zipOutputStream.putNextEntry(entry);

            int count;
            while ((count = input.read(data, 0, BUFFER_SIZE)) != -1) {
                zipOutputStream.write(data, 0, count);
            }
            input.close();
        }
    }

}

From source file:Main.java

public static void zip(String zipFileName, String[] zipEntries) {

    try (ZipOutputStream zos = new ZipOutputStream(
            new BufferedOutputStream(new FileOutputStream(zipFileName)))) {

        // Set the compression level to best compression
        zos.setLevel(Deflater.BEST_COMPRESSION);

        for (int i = 0; i < zipEntries.length; i++) {
            File entryFile = new File(zipEntries[i]);
            if (!entryFile.exists()) {
                System.out.println("The entry file  " + entryFile.getAbsolutePath() + "  does  not  exist");
                System.out.println("Aborted   processing.");
                return;
            }/*from   w w w  .j  a va  2  s.co  m*/
            ZipEntry ze = new ZipEntry(zipEntries[i]);
            zos.putNextEntry(ze);
            addEntryContent(zos, zipEntries[i]);
            zos.closeEntry();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.fredhopper.core.connector.index.FileUtils.java

public static void addToZipFile(final File file, final ZipOutputStream zos) throws IOException {

    final FileInputStream fis = new FileInputStream(file);
    final ZipEntry zipEntry = new ZipEntry(file.getName());
    zos.putNextEntry(zipEntry);/* w  w  w . j av  a  2s . c  om*/

    final byte[] bytes = new byte[1024];
    int length;
    while ((length = fis.read(bytes)) >= 0) {
        zos.write(bytes, 0, length);
    }

    zos.closeEntry();
    fis.close();
}

From source file:Main.java

private static void compressDir(File srcFile, ZipOutputStream out, String destPath) throws IOException {
    if (srcFile.isDirectory()) {
        File subfile[] = srcFile.listFiles();
        for (int i = 0; i < subfile.length; i++) {
            compressDir(subfile[i], out, destPath);
        }//from  w  w w  . j a va2  s .  c  o  m
    } else {
        InputStream in = new FileInputStream(srcFile);
        String name = srcFile.getAbsolutePath().replace(destPath, "");
        if (name.startsWith("\\"))
            name = name.substring(1);
        ZipEntry entry = new ZipEntry(name);
        entry.setSize(srcFile.length());
        entry.setTime(srcFile.lastModified());
        out.putNextEntry(entry);
        int len = -1;
        byte buf[] = new byte[1024];
        while ((len = in.read(buf, 0, 1024)) != -1)
            out.write(buf, 0, len);

        out.closeEntry();
        in.close();
    }
}

From source file:es.caib.sgtsic.util.Zips.java

public static DataHandler generateZip(Map<String, DataHandler> documents) throws IOException {

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

    for (String key : documents.keySet()) {

        InputStream is = new ByteArrayInputStream(DataHandlers.dataHandlerToByteArray(documents.get(key)));
        ZipEntry zipEntry = new ZipEntry(key);
        zip.putNextEntry(zipEntry);/* w  w w. j  a  v  a  2s .c om*/
        IOUtils.copy(is, zip);
        zip.closeEntry();
        is.close();
    }

    zip.close();
    baos.close();

    return DataHandlers.byteArrayToDataHandler(baos.toByteArray(), "application/zip");

}

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("Not a directory:  " + dir);
    String[] entries = d.list();/*ww w  .  j  av  a  2s. c o m*/
    byte[] buffer = new byte[4096]; // Create a buffer for copying
    int bytesRead;

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

    for (int i = 0; i < entries.length; i++) {
        File f = new File(d, entries[i]);
        if (f.isDirectory())
            continue;//Ignore directory
        FileInputStream in = new FileInputStream(f); // Stream to read file
        ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
        out.putNextEntry(entry); // Store entry
        while ((bytesRead = in.read(buffer)) != -1)
            out.write(buffer, 0, bytesRead);
        in.close();
    }
    out.close();
}

From source file:com.splout.db.common.CompressorUtil.java

public static void createZip(File dir, File out, IOFileFilter filefilter, IOFileFilter dirFilter)
        throws IOException {
    Collection<File> files = FileUtils.listFiles(dir, filefilter, dirFilter);

    out.delete();//w  w  w  .j  a  v a 2 s  .com
    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out));
    byte[] buf = new byte[1024];
    for (File f : files) {
        ZipEntry ze = new ZipEntry(getRelativePath(f, dir));
        zos.putNextEntry(ze);
        InputStream is = new FileInputStream(f);
        int cnt;
        while ((cnt = is.read(buf)) >= 0) {
            zos.write(buf, 0, cnt);
        }
        is.close();
        zos.flush();
        zos.closeEntry();
    }
    zos.close();
}

From source file:Main.java

public static String zip(String filename) throws IOException {
    BufferedInputStream origin = null;
    Integer BUFFER_SIZE = 20480;//www . j  av a  2  s  .  c o  m

    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

        File flockedFilesFolder = new File(
                Environment.getExternalStorageDirectory() + File.separator + "FlockLoad");
        System.out.println("FlockedFileDir: " + flockedFilesFolder);
        String uncompressedFile = flockedFilesFolder.toString() + "/" + filename;
        String compressedFile = flockedFilesFolder.toString() + "/" + "flockZip.zip";
        ZipOutputStream out = new ZipOutputStream(
                new BufferedOutputStream(new FileOutputStream(compressedFile)));
        out.setLevel(9);
        try {
            byte data[] = new byte[BUFFER_SIZE];
            FileInputStream fi = new FileInputStream(uncompressedFile);
            System.out.println("Filename: " + uncompressedFile);
            System.out.println("Zipfile: " + compressedFile);
            origin = new BufferedInputStream(fi, BUFFER_SIZE);
            try {
                ZipEntry entry = new ZipEntry(
                        uncompressedFile.substring(uncompressedFile.lastIndexOf("/") + 1));
                out.putNextEntry(entry);
                int count;
                while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
                    out.write(data, 0, count);
                }
            } finally {
                origin.close();
            }
        } finally {
            out.close();
        }
        return "flockZip.zip";
    }
    return null;
}

From source file:Main.java

public void doZip(String filename, String zipfilename) throws Exception {
    byte[] buf = new byte[1024];
    FileInputStream fis = new FileInputStream(filename);
    fis.read(buf, 0, buf.length);/* w ww .  jav a  2  s.  co  m*/

    CRC32 crc = new CRC32();
    ZipOutputStream s = new ZipOutputStream((OutputStream) new FileOutputStream(zipfilename));
    s.setLevel(6);

    ZipEntry entry = new ZipEntry(filename);
    entry.setSize((long) buf.length);
    crc.reset();
    crc.update(buf);
    entry.setCrc(crc.getValue());
    s.putNextEntry(entry);
    s.write(buf, 0, buf.length);
    s.finish();
    s.close();
}