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:com.mcleodmoores.mvn.natives.UnpackDependenciesMojoTest.java

private static Artifact createArtifact(final File tmp, final String type, final String member)
        throws IOException {
    final File zipFile = new File(tmp, type + ".zip");
    try (final FileOutputStream out = new FileOutputStream(zipFile)) {
        final ZipOutputStream zipStream = new ZipOutputStream(out);
        final ZipEntry license = new ZipEntry("LICENSE");
        zipStream.putNextEntry(license);
        zipStream.write(26);// w  w  w . ja v a2  s . com
        zipStream.closeEntry();
        final ZipEntry payload = new ZipEntry(member);
        zipStream.putNextEntry(payload);
        zipStream.write(26);
        zipStream.closeEntry();
        zipStream.close();
    }
    final Artifact artifact = Mockito.mock(Artifact.class);
    Mockito.when(artifact.getType()).thenReturn(type);
    Mockito.when(artifact.getGroupId()).thenReturn("uk.co.beerdragon");
    Mockito.when(artifact.getArtifactId()).thenReturn("test-" + type);
    Mockito.when(artifact.getVersion()).thenReturn("SNAPSHOT");
    Mockito.when(artifact.getFile()).thenReturn(zipFile);
    return artifact;
}

From source file:com.baasbox.util.Util.java

public static void createZipFile(String path, File... files) {
    if (BaasBoxLogger.isDebugEnabled())
        BaasBoxLogger.debug("Zipping into:" + path);
    ZipOutputStream zip = null;
    FileOutputStream dest = null;
    try {//  w ww . j  a  v  a 2 s  . c om
        File f = new File(path);
        dest = new FileOutputStream(f);
        zip = new ZipOutputStream(new BufferedOutputStream(dest));
        for (File file : files) {
            zip.putNextEntry(new ZipEntry(file.getName()));
            zip.write(FileUtils.readFileToByteArray(file));
            zip.closeEntry();
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("Unable to create zip file");
    } finally {
        try {
            if (zip != null)
                zip.close();
            if (dest != null)
                dest.close();

        } catch (Exception ioe) {
            //Nothing to do
        }
    }
}

From source file:com.formkiq.core.util.Zips.java

/**
 * Create Zip file.//w ww. j ava  2s .  co m
 * @param map {@link Map}
 * @return byte[] zip file
 * @throws IOException IOException
 */
public static byte[] zipFile(final Map<String, byte[]> map) throws IOException {

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

    for (Map.Entry<String, byte[]> e : map.entrySet()) {

        String name = e.getKey();
        byte[] data = e.getValue();

        ZipEntry ze = new ZipEntry(name);
        zip.putNextEntry(ze);

        zip.write(data, 0, data.length);
        zip.closeEntry();
    }

    zip.finish();

    byte[] bytes = bs.toByteArray();

    IOUtils.closeQuietly(bs);
    IOUtils.closeQuietly(zip);

    return bytes;
}

From source file:Main.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws IOException {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip, false);
    } else {/* www. j  ava  2 s .  c om*/
        byte[] buf = new byte[1024];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        try {
            zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
            while ((len = in.read(buf)) > 0) {
                zip.write(buf, 0, len);
            }
        } finally {
            in.close();
        }
    }
}

From source file:Main.java

public static void zipMutiCompress(File[] srcfile, File destFile) {
    byte[] buf = new byte[BUFFERED_SIZE];
    try {//from   w  ww.jav  a2s . co m
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile));
        if (null != srcfile && srcfile.length > 0) {
            for (int i = 0; i < srcfile.length; i++) {
                File file = srcfile[i];
                if (null != file) {
                    FileInputStream in = new FileInputStream(file);
                    out.putNextEntry(new ZipEntry(file.getName()));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    out.closeEntry();
                    in.close();
                }
            }
        }
        out.close();
    } catch (IOException e) {

    }
}

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.ja  va 2 s.  c  om*/
    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.recomdata.transmart.data.export.util.ZipUtil.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception {

    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {/*from w  w w  .  j av  a  2s . c om*/
        byte[] buf = new byte[BUFFER_SIZE];
        int len;
        FileInputStream in = new FileInputStream(srcFile);
        zip.putNextEntry(new ZipEntry(path + "/" + folder.getName()));
        while ((len = in.read(buf)) > 0) {
            zip.write(buf, 0, len);
        }
    }
}

From source file:net.bpelunit.util.ZipUtil.java

public static void zipDirectory(File directory, File zipFile) throws IOException {
    @SuppressWarnings("unchecked")
    Collection<File> files = FileUtils.listFiles(directory, null, true);

    FileOutputStream fzos = null;
    ZipOutputStream zos = null;
    try {//from w w w. j  a v a  2 s  .  co m
        fzos = new FileOutputStream(zipFile);
        zos = new ZipOutputStream(fzos);

        for (File f : files) {
            String fileNameInZIP = directory.toURI().relativize(f.toURI()).getPath();
            ZipEntry zipEntry = new ZipEntry(fileNameInZIP);
            zos.putNextEntry(zipEntry);
            FileInputStream fileInputStream = new FileInputStream(f);
            try {
                IOUtils.copy(fileInputStream, zos);
            } finally {
                IOUtils.closeQuietly(fileInputStream);
            }
        }
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(fzos);
    }
}

From source file:Main.java

public static void zipChildren(File folder, String prefix, ZipOutputStream out) throws IOException {
    File[] files = folder.listFiles();
    if (files == null)
        return;//from ww  w  . ja  va2  s  .c  o m
    for (File file : files) {
        if (file.isFile()) {
            String name = prefix + file.getName();
            ZipEntry entry = new ZipEntry(name);
            entry.setTime(file.lastModified());
            out.putNextEntry(entry);
            loadFromFile(file, out);
            out.closeEntry();
        } else if (file.isDirectory()) {
            zipChildren(file, prefix + file.getName() + "/", out);
        }
    }

}

From source file:com.amalto.core.storage.hibernate.TypeMapping.java

private static Object _serializeValue(Object value, FieldMetadata sourceField, FieldMetadata targetField,
        Session session) {//from  ww w.  ja v a 2  s. c om
    if (targetField == null) {
        return value;
    }
    if (!targetField.isMany()) {
        Boolean targetZipped = targetField.<Boolean>getData(MetadataRepository.DATA_ZIPPED);
        Boolean sourceZipped = sourceField.<Boolean>getData(MetadataRepository.DATA_ZIPPED);
        if (sourceZipped == null && Boolean.TRUE.equals(targetZipped)) {
            try {
                ByteArrayOutputStream characters = new ByteArrayOutputStream();
                OutputStream bos = new Base64OutputStream(characters);
                ZipOutputStream zos = new ZipOutputStream(bos);
                ZipEntry zipEntry = new ZipEntry("content"); //$NON-NLS-1$
                zos.putNextEntry(zipEntry);
                zos.write(String.valueOf(value).getBytes("UTF-8")); //$NON-NLS-1$
                zos.closeEntry();
                zos.flush();
                zos.close();
                return new String(characters.toByteArray());
            } catch (IOException e) {
                throw new RuntimeException("Unexpected compression exception", e); //$NON-NLS-1$
            }
        }
        String targetSQLType = targetField.getType().getData(TypeMapping.SQL_TYPE);
        if (targetSQLType != null && SQL_TYPE_CLOB.equals(targetSQLType)) {
            if (value != null) {
                return Hibernate.getLobCreator(session).createClob(String.valueOf(value));
            } else {
                return null;
            }
        }
    }
    return value;
}