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:com.gelakinetic.mtgfam.helpers.ZipUtils.java

/**
 * Zips all files specified in an ArrayList into a given file
 *
 * @param zipFile The file to zip files into
 * @param files   The files to be zipped
 * @param context The application context, for getting files and the like
 * @throws IOException IOException Thrown if something goes wrong with zipping and reading
 *//*from  w ww  .j  a  va  2 s. c om*/
private static void zipIt(File zipFile, ArrayList<File> files, Context context) throws IOException {

    byte[] buffer = new byte[1024];

    FileOutputStream fos = new FileOutputStream(zipFile);
    ZipOutputStream zos = new ZipOutputStream(fos);
    assert context.getFilesDir() != null;
    for (File file : files) {
        ZipEntry ze = new ZipEntry(file.getName());
        zos.putNextEntry(ze);

        FileInputStream in = new FileInputStream(file);

        int len;
        while ((len = in.read(buffer)) > 0) {
            zos.write(buffer, 0, len);
        }
        in.close();
    }

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

From source file:Main.java

/**
 * Compress files to a zip//from  w w w  .  j a va2  s. c o  m
 * @param zip
 * @param files
 * @throws IOException 
 */
public static void compressToZip(File zip, File[] files) throws IOException {
    byte data[] = new byte[BUFFER];
    FileOutputStream fozip = new FileOutputStream(zip);
    ZipOutputStream zo = new ZipOutputStream(new BufferedOutputStream(fozip));
    for (int i = 0; i < files.length; i++) {
        System.out.println("Adding:" + files[i]);
        FileInputStream fi = new FileInputStream(files[i]);
        BufferedInputStream origin = new BufferedInputStream(fi, BUFFER);
        ZipEntry zipentry = new ZipEntry(files[i].getName());
        zo.putNextEntry(zipentry);
        int count;
        while ((count = origin.read(data, 0, BUFFER)) != -1) {
            zo.write(data, 0, count);
        }
        origin.close();
    }
    zo.close();
}

From source file:Main.java

private static final void zip(File directory, File base, ZipOutputStream zos) throws IOException {
    File[] files = directory.listFiles();
    byte[] buffer = new byte[8192];
    int read = 0;
    for (int i = 0, n = files.length; i < n; i++) {
        if (files[i].isDirectory()) {
            zip(files[i], base, zos);/*from   www. j  av  a  2 s . c  o  m*/
        } else {
            FileInputStream in = new FileInputStream(files[i]);
            ZipEntry entry = new ZipEntry(files[i].getPath().substring(base.getPath().length() + 1));
            zos.putNextEntry(entry);
            while (-1 != (read = in.read(buffer))) {
                zos.write(buffer, 0, read);
            }
            in.close();
        }
    }
}

From source file:org.duracloud.common.util.IOUtil.java

/**
 * Adds the specified file to the zip output stream.
 *
 * @param file/*w ww .  ja  v a2s  .co m*/
 * @param zipOs
 */
public static void addFileToZipOutputStream(File file, ZipOutputStream zipOs) throws IOException {
    String fileName = file.getName();
    try (FileInputStream fos = new FileInputStream(file)) {
        ZipEntry zipEntry = new ZipEntry(fileName);
        zipEntry.setSize(file.length());
        zipEntry.setTime(System.currentTimeMillis());
        zipOs.putNextEntry(zipEntry);
        byte[] buf = new byte[1024];
        int bytesRead;
        while ((bytesRead = fos.read(buf)) > 0) {
            zipOs.write(buf, 0, bytesRead);
        }
        zipOs.closeEntry();
        fos.close();
    }
}

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();//from   w  ww .  j  a v  a  2 s  .  co m
            }
        }
        out.close();
    }
    return zipfile;
}

From source file:com.compomics.pladipus.core.control.util.ZipUtils.java

static private void addFileToZip(String path, String srcFile, ZipOutputStream zip)
        throws UnspecifiedPladipusException, FileNotFoundException, IOException {
    File folder = new File(srcFile);
    if (folder.isDirectory()) {
        addFolderToZip(path, srcFile, zip);
    } else {/*from ww  w.  j  a  v  a  2s. c o  m*/
        byte[] buf = new byte[1024];
        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:com.rackspacecloud.client.cloudfiles.sample.FilesCopy.java

public static File zipFile(File f) throws IOException {
    byte[] buf = new byte[1024];
    int len;//from  www. j a v a  2 s . c o  m

    // Create the ZIP file
    String filenameWithZipExt = f.getName() + ZIPEXTENSION;
    File zippedFile = new File(FilenameUtils.concat(SYSTEM_TMP.getAbsolutePath(), filenameWithZipExt));

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zippedFile));
    FileInputStream in = new FileInputStream(f);

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

    // Transfer bytes from the file to the ZIP file
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    // Complete the entry
    out.closeEntry();
    out.flush();

    in.close();
    out.close();

    return zippedFile;
}

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

/**
 * Utility method for zipping a list of files
 * //from   w ww.j av  a2  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.juancarlosroot.threads.SimulatedUser.java

public static void addToZipFile(String fileName, ZipOutputStream zos)
        throws FileNotFoundException, IOException {
    File file = new File(fileName);
    FileInputStream fis = new FileInputStream(file);
    ZipEntry zipEntry = new ZipEntry(fileName);
    zos.putNextEntry(zipEntry);/* w  ww .  jav  a 2  s  .c om*/

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

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

From source file:com.danimahardhika.android.helpers.core.FileHelper.java

@Nullable
public static String createZip(@NonNull List<String> files, File file) {
    try {/*from  w w w  .j  ava 2 s  .  co m*/
        final int BUFFER = 2048;
        BufferedInputStream origin;
        FileOutputStream dest = new FileOutputStream(file);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));

        byte data[] = new byte[BUFFER];
        for (int i = 0; i < files.size(); i++) {
            FileInputStream fi = new FileInputStream(files.get(i));
            origin = new BufferedInputStream(fi, BUFFER);

            ZipEntry entry = new ZipEntry(files.get(i).substring(files.get(i).lastIndexOf("/") + 1));
            out.putNextEntry(entry);
            int count;

            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }

        out.close();
        return file.toString();
    } catch (Exception ignored) {
    }
    return null;
}