Example usage for java.util.zip ZipOutputStream write

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

Introduction

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

Prototype

public synchronized void write(byte[] b, int off, int len) throws IOException 

Source Link

Document

Writes an array of bytes to the current ZIP entry data.

Usage

From source file:org.apache.gobblin.service.modules.orchestration.AzkabanClientTest.java

private static void addDirToZipArchive(ZipOutputStream zos, File fileToZip, String parentDirectoryName)
        throws IOException {
    if (fileToZip == null || !fileToZip.exists()) {
        return;//from  w w w  .jav  a 2s  .  c  o m
    }

    String zipEntryName = fileToZip.getName();
    if (parentDirectoryName != null && !parentDirectoryName.isEmpty()) {
        zipEntryName = parentDirectoryName + "/" + fileToZip.getName();
    }

    if (fileToZip.isDirectory()) {
        for (File file : fileToZip.listFiles()) {
            addDirToZipArchive(zos, file, zipEntryName);
        }
    } else {
        byte[] buffer = new byte[1024];
        FileInputStream fis = new FileInputStream(fileToZip);
        zos.putNextEntry(new ZipEntry(zipEntryName));
        int length;
        while ((length = fis.read(buffer)) > 0) {
            zos.write(buffer, 0, length);
        }
        zos.closeEntry();
        fis.close();
    }
}

From source file:edu.umd.cs.submit.CommandLineSubmit.java

/**
 * @param p/*from   w w  w .j  a v  a  2s  . c  o m*/
 * @param find
 * @param files
 * @param userProps
 * @return
 * @throws IOException
 * @throws FileNotFoundException
 */
public static MultipartPostMethod createFilePost(Properties p, FindAllFiles find, Collection<File> files,
        Properties userProps) throws IOException, FileNotFoundException {
    // ========================== assemble zip file in byte array
    // ==============================
    String loginName = userProps.getProperty("loginName");
    String classAccount = userProps.getProperty("classAccount");
    String from = classAccount;
    if (loginName != null && !loginName.equals(classAccount))
        from += "/" + loginName;
    System.out.println(" submitted by " + from);
    System.out.println();
    System.out.println("Submitting the following files");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(4096);
    byte[] buf = new byte[4096];
    ZipOutputStream zipfile = new ZipOutputStream(bytes);
    zipfile.setComment("zipfile for CommandLineTurnin, version " + VERSION);
    for (File resource : files) {
        if (resource.isDirectory())
            continue;
        String relativePath = resource.getCanonicalPath().substring(find.rootPathLength + 1);
        System.out.println(relativePath);
        ZipEntry entry = new ZipEntry(relativePath);
        entry.setTime(resource.lastModified());

        zipfile.putNextEntry(entry);
        InputStream in = new FileInputStream(resource);
        try {
            while (true) {
                int n = in.read(buf);
                if (n < 0)
                    break;
                zipfile.write(buf, 0, n);
            }
        } finally {
            in.close();
        }
        zipfile.closeEntry();

    } // for each file
    zipfile.close();

    MultipartPostMethod filePost = new MultipartPostMethod(p.getProperty("submitURL"));

    p.putAll(userProps);
    // add properties
    for (Map.Entry<?, ?> e : p.entrySet()) {
        String key = (String) e.getKey();
        String value = (String) e.getValue();
        if (!key.equals("submitURL"))
            filePost.addParameter(key, value);
    }
    filePost.addParameter("submitClientTool", "CommandLineTool");
    filePost.addParameter("submitClientVersion", VERSION);
    byte[] allInput = bytes.toByteArray();
    filePost.addPart(new FilePart("submittedFiles", new ByteArrayPartSource("submit.zip", allInput)));
    return filePost;
}

From source file:com.matze5800.paupdater.Functions.java

public static void addFilesToExistingZip(File zipFile, File[] files) throws IOException {
    File tempFile = new File(Environment.getExternalStorageDirectory() + "/pa_updater", "temp_kernel.zip");
    tempFile.delete();//  ww w. j  a v  a2  s .c o m

    boolean renameOk = zipFile.renameTo(tempFile);
    if (!renameOk) {
        throw new RuntimeException(
                "could not rename the file " + zipFile.getAbsolutePath() + " to " + tempFile.getAbsolutePath());
    }
    byte[] buf = new byte[1024];

    ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile));
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile));

    ZipEntry entry = zin.getNextEntry();
    while (entry != null) {
        String name = entry.getName();
        boolean notInFiles = true;
        for (File f : files) {
            if (f.getName().equals(name)) {
                notInFiles = false;
                break;
            }
        }
        if (notInFiles) {

            out.putNextEntry(new ZipEntry(name));

            int len;
            while ((len = zin.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        }
        entry = zin.getNextEntry();
    }
    zin.close();

    for (int i = 0; i < files.length; i++) {
        InputStream in = new FileInputStream(files[i]);
        out.putNextEntry(new ZipEntry(files[i].getName()));

        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        out.closeEntry();
        in.close();
    }
    out.close();
    tempFile.delete();
}

From source file:de.brendamour.jpasskit.signing.PKSigningUtil.java

private static final void zip(final File directory, final File base, final ZipOutputStream zipOutputStream)
        throws IOException {
    File[] files = directory.listFiles();
    byte[] buffer = new byte[ZIP_BUFFER_SIZE];
    int read = 0;
    for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory()) {
            zip(files[i], base, zipOutputStream);
        } else {//from w  w  w .  ja  va2 s . c om
            FileInputStream fileInputStream = new FileInputStream(files[i]);
            ZipEntry entry = new ZipEntry(getRelativePathOfZipEntry(files[i], base));
            zipOutputStream.putNextEntry(entry);
            while (-1 != (read = fileInputStream.read(buffer))) {
                zipOutputStream.write(buffer, 0, read);
            }
            fileInputStream.close();
        }
    }
}

From source file:game.com.HandleDownloadFolderServlet.java

public static void addDirToZipArchive(ZipOutputStream zos, File fileToZip, String parrentDirectoryName)
        throws Exception {
    if (fileToZip == null || !fileToZip.exists()) {
        return;/*from  www.  j  av a 2s . c o m*/
    }

    String zipEntryName = fileToZip.getName();
    if (parrentDirectoryName != null && !parrentDirectoryName.isEmpty()) {
        zipEntryName = parrentDirectoryName + "/" + fileToZip.getName();
    }

    if (fileToZip.isDirectory()) {
        System.out.println("+" + zipEntryName);
        for (File file : fileToZip.listFiles()) {
            addDirToZipArchive(zos, file, zipEntryName);
        }
    } else {
        System.out.println("   " + zipEntryName);
        byte[] buffer = new byte[1024];
        FileInputStream fis = new FileInputStream(fileToZip);
        zos.putNextEntry(new ZipEntry(zipEntryName));
        int length;
        while ((length = fis.read(buffer)) > 0) {
            zos.write(buffer, 0, length);
        }
        zos.closeEntry();
        fis.close();
    }
}

From source file:mpimp.assemblxweb.util.J5FileUtils.java

private static void zipDirectoryRecursively(String dirPath, BufferedInputStream origin, int BUFFER,
        ZipOutputStream out, byte[] data, String parentDirName) throws Exception {
    File directory = new File(dirPath);
    File content[] = directory.listFiles();

    for (int i = 0; i < content.length; i++) {
        if (content[i].isDirectory()) {
            String parentPath = parentDirName + File.separator + content[i].getName();
            zipDirectoryRecursively(content[i].getAbsolutePath(), origin, BUFFER, out, data, parentPath);
        } else {/*from w w w  .  j a v  a  2s .co  m*/
            String filePathInDirectory = parentDirName + File.separator + content[i].getName();
            FileInputStream in = new FileInputStream(content[i].getAbsolutePath());
            origin = new BufferedInputStream(in, BUFFER);
            ZipEntry zipEntry = new ZipEntry(filePathInDirectory);
            out.putNextEntry(zipEntry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
            in.close();
        }
    }
}

From source file:org.apache.sling.tooling.support.install.impl.InstallServlet.java

public static void zipDir(final File sourceDir, final ZipOutputStream zos, final String path)
        throws IOException {
    final byte[] readBuffer = new byte[8192];
    int bytesIn = 0;

    for (final File f : sourceDir.listFiles()) {
        if (f.isDirectory()) {
            final String prefix = path + f.getName() + "/";
            zos.putNextEntry(new ZipEntry(prefix));
            zipDir(f, zos, prefix);//ww  w . jav  a2s  . c om
        } else {
            final String entry = path + f.getName();
            if (!JarFile.MANIFEST_NAME.equals(entry)) {
                final FileInputStream fis = new FileInputStream(f);
                try {
                    final ZipEntry anEntry = new ZipEntry(entry);
                    zos.putNextEntry(anEntry);
                    while ((bytesIn = fis.read(readBuffer)) != -1) {
                        zos.write(readBuffer, 0, bytesIn);
                    }
                } finally {
                    fis.close();
                }
            }
        }
    }
}

From source file:com.thejustdo.util.Utils.java

/**
 * Creates a ZIP file containing all the files inside a directory.
 * @param location Directory to read.//from  w w  w .  j a  va  2  s .c om
 * @param pathname Name and path where to store the file.
 * @throws FileNotFoundException If can't find the initial directory.
 * @throws IOException If can't read/write.
 */
public static void zipDirectory(File location, File pathname) throws FileNotFoundException, IOException {

    BufferedInputStream origin;
    FileOutputStream dest = new FileOutputStream(pathname);
    ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
    byte data[] = new byte[2048];

    // 1. Get a list of files from current directory
    String files[] = location.list();
    File f;

    // 2. Adding each file to the zip-set.
    for (String s : files) {
        log.info(String.format("Adding: %s", s));
        f = new File(location, s);
        FileInputStream fi = new FileInputStream(f);
        origin = new BufferedInputStream(fi, 2048);
        ZipEntry entry = new ZipEntry(location.getName() + File.separator + s);
        out.putNextEntry(entry);
        int count;
        while ((count = origin.read(data, 0, 2048)) != -1) {
            out.write(data, 0, count);
        }
        origin.close();
    }
    out.close();
}

From source file:com.nridge.core.base.std.FilUtl.java

/**
 * Compresses the input path name into a ZIP output file stream.  The
 * method will recursively process all files and sub folders within
 * the input path file./* w  w w  . ja  v  a2s  .co m*/
 *
 * @param aPathName Identifies the name of the folder to compress.
 * @param aZipOutStream A previously opened ZIP output file stream.
 * @throws IOException Related to opening the file streams and
 * related read/write operations.
 */
static public void zipPath(String aPathName, ZipOutputStream aZipOutStream) throws IOException {
    int byteCount;
    byte[] ioBuf;
    String[] fileList;
    File zipFile, inFile;
    FileInputStream fileIn;

    zipFile = new File(aPathName);
    if (!zipFile.isDirectory())
        return;

    fileList = zipFile.list();
    if (fileList == null)
        return;

    for (String fileName : fileList) {
        inFile = new File(aPathName, fileName);
        if (inFile.isDirectory())
            zipPath(inFile.getPath(), aZipOutStream);
        else {
            ioBuf = new byte[FILE_IO_BUFFER_SIZE];
            fileIn = new FileInputStream(inFile);
            aZipOutStream.putNextEntry(new ZipEntry(inFile.getName()));
            byteCount = fileIn.read(ioBuf);
            while (byteCount > 0) {
                aZipOutStream.write(ioBuf, 0, byteCount);
                byteCount = fileIn.read(ioBuf);
            }
            fileIn.close();
        }
    }
}

From source file:com.jaspersoft.jasperserver.jrsh.common.ZipUtil.java

protected static void addFiles(ZipOutputStream zos, String folder, String baseFolder) throws Exception {
    File file = new File(folder);
    if (file.exists()) {
        if (file.isDirectory()) {
            if (!folder.equalsIgnoreCase(baseFolder)) {
                String entryName = folder.substring(baseFolder.length() + 1, folder.length()) + separatorChar;
                ZipEntry zipEntry = new ZipEntry(entryName);
                zos.putNextEntry(zipEntry);
            }//from   www.  ja v a2  s  . c o  m
            File files[] = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    addFiles(zos, f.getAbsolutePath(), baseFolder);
                }
            }
        } else {
            String entryName = folder.substring(baseFolder.length() + 1, folder.length());
            ZipEntry zipEntry = new ZipEntry(entryName);
            zos.putNextEntry(zipEntry);

            try (FileInputStream in = new FileInputStream(folder)) {
                int len;
                byte buf[] = new byte[1024];
                while ((len = in.read(buf)) > 0) {
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
            }
        }
    }
}