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:Main.java

static void zipFile(File zipfile, ZipOutputStream zos, String name) throws IOException {
    // if we reached here, the File object f was not a directory 
    // create a FileInputStream on top of f

    FileInputStream fis = new FileInputStream(zipfile);
    try {//from  ww  w  .jav  a2 s  .  c om
        // create a new zip entry 
        ZipEntry anEntry = new ZipEntry(name);
        if (DEBUG)
            System.out.println("Add file : " + name);
        // place the zip entry in the ZipOutputStream object
        zos.putNextEntry(anEntry);
        // now write the content of the file to the
        // ZipOutputStream
        byte[] readBuffer = new byte[BUFFER_SIZE];
        for (int bytesIn = fis.read(readBuffer); bytesIn != -1; bytesIn = fis.read(readBuffer)) {
            zos.write(readBuffer, 0, bytesIn);
        }
    } finally {
        // close the Stream
        fis.close();
    }
}

From source file:gov.nih.nci.caintegrator.common.Cai2Util.java

private static void addFile(File curFile, ZipOutputStream out, int index) throws IOException {
    byte[] tmpBuf = new byte[BUFFER_SIZE];
    FileInputStream in = new FileInputStream(curFile);
    String relativePathName = curFile.getPath().substring(index);
    out.putNextEntry(new ZipEntry(relativePathName));
    int len;/* w  w w. j a  v a2  s . co m*/
    while ((len = in.read(tmpBuf)) > 0) {
        out.write(tmpBuf, 0, len);
    }
    // Complete the entry
    out.closeEntry();
    in.close();
}

From source file:edu.isi.wings.portal.classes.StorageHandler.java

private static void zipAndStream(File dir, ZipOutputStream zos, String prefix) throws Exception {
    byte bytes[] = new byte[2048];
    for (File file : dir.listFiles()) {
        if (file.isDirectory())
            StorageHandler.zipAndStream(file, zos, prefix + file.getName() + "/");
        else {/*w w  w  .  j  av  a 2  s . c o m*/
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());
            BufferedInputStream bis = new BufferedInputStream(fis);
            zos.putNextEntry(new ZipEntry(prefix + file.getName()));
            int bytesRead;
            while ((bytesRead = bis.read(bytes)) != -1) {
                zos.write(bytes, 0, bytesRead);
            }
            zos.closeEntry();
            bis.close();
            fis.close();
        }
    }
}

From source file:Main.java

public static String zip(String filename) throws IOException {
    BufferedInputStream origin = null;
    Integer BUFFER_SIZE = 20480;//from w ww. j a  v a 2  s .c om

    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:com.mosso.client.cloudfiles.sample.FilesCopy.java

/**
 *
 * @param folder/* w w  w.ja  va2s  .c o  m*/
 * @return null if the input is not a folder otherwise a zip file containing all the files in the folder with nested folders skipped.
 * @throws IOException
 */
public static File zipFolder(File folder) throws IOException {
    byte[] buf = new byte[1024];
    int len;

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

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

    if (folder.isDirectory()) {
        File[] files = folder.listFiles();

        for (File f : files) {
            if (!f.isDirectory()) {
                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();
                in.close();
            } else
                logger.warn("Skipping nested folder: " + f.getAbsoluteFile());
        }

        out.flush();
        out.close();
    } else {
        logger.warn("The folder name supplied is not a folder!");
        System.err.println("The folder name supplied is not a folder!");
        return null;
    }

    return zippedFile;
}

From source file:com.mc.printer.model.utils.ZipHelper.java

private static void writeZip(File file, String parentPath, ZipOutputStream zos) {
    if (file.exists()) {
        if (file.isDirectory()) {//?
            parentPath += file.getName() + File.separator;
            File[] files = file.listFiles();
            for (File f : files) {
                writeZip(f, parentPath, zos);
            }/* w  ww  .j  a  va2s  .com*/
        } else {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);
                ZipEntry ze = new ZipEntry(parentPath + file.getName());
                zos.putNextEntry(ze);
                byte[] content = new byte[1024];
                int len;
                while ((len = fis.read(content)) != -1) {
                    zos.write(content, 0, len);
                    zos.flush();
                }

            } catch (FileNotFoundException e) {
                log.error("create zip file failed.", e);
            } catch (IOException e) {
                log.error("create zip file failed.", e);
            } finally {
                try {
                    if (fis != null) {
                        fis.close();
                    }
                } catch (IOException e) {
                    log.error("create zip file failed.", e);
                }
            }
        }
    }
}

From source file:org.eclipse.emf.emfstore.internal.common.model.util.FileUtil.java

private static void zip(File current, String rootPath, ZipOutputStream zipStream, byte[] buffer)
        throws IOException {
    if (current.isDirectory()) {
        for (final File file : current.listFiles()) {
            if (!".".equals(file.getName()) && !"..".equals(file.getName())) { //$NON-NLS-1$ //$NON-NLS-2$
                zip(file, rootPath, zipStream, buffer);
            }/*from   w w  w  .  ja va  2s .  com*/
        }
    } else if (current.isFile()) {
        zipStream.putNextEntry(new ZipEntry(current.getPath().replace(rootPath, StringUtils.EMPTY)));
        final FileInputStream file = new FileInputStream(current);
        int read;
        while ((read = file.read(buffer)) != -1) {
            zipStream.write(buffer, 0, read);
        }
        zipStream.closeEntry();
        file.close();
    } else {
        throw new IllegalStateException();
    }
}

From source file:ObjectLabEnterpriseSoftware.FileManager.java

public static void zipFile(File inputFile, String parentName, ZipOutputStream zipOutputStream)
        throws IOException {

    // A ZipEntry represents a file entry in the zip archive
    // We name the ZipEntry after the original file's name

    ZipEntry zipEntry = new ZipEntry(parentName + inputFile.getName());

    zipOutputStream.putNextEntry(zipEntry);

    FileInputStream fileInputStream = new FileInputStream(inputFile);

    byte[] buf = new byte[1024];

    int bytesRead;

    // Read the input file by chucks of 1024 bytes
    // and write the read bytes to the zip stream
    while ((bytesRead = fileInputStream.read(buf)) > 0) {

        zipOutputStream.write(buf, 0, bytesRead);
    }/*from w  w  w  .  j a v a2  s  . c  om*/
    // close ZipEntry to store the stream to the file
    zipOutputStream.closeEntry();

    // System.out.println("Regular file :" + parentName+inputFile.getName() +" is zipped to archive :"+ZIPPED_FOLDER);
}

From source file:FileUtil.java

/**
 * Zip up a directory path//  w  w  w .j a  v  a2 s .  co m
 * @param directory
 * @param zos
 * @param path
 * @throws IOException
 */
public static void zipDir(String directory, ZipOutputStream zos, String path) throws IOException {
    File zipDir = new File(directory);
    // get a listing of the directory content
    String[] dirList = zipDir.list();
    byte[] readBuffer = new byte[2156];
    int bytesIn = 0;
    // loop through dirList, and zip the files
    for (int i = 0; i < dirList.length; i++) {
        File f = new File(zipDir, dirList[i]);
        if (f.isDirectory()) {
            String filePath = f.getPath();
            zipDir(filePath, zos, path + f.getName() + "/");
            continue;
        }
        FileInputStream fis = new FileInputStream(f);
        try {
            ZipEntry anEntry = new ZipEntry(path + f.getName());
            zos.putNextEntry(anEntry);
            bytesIn = fis.read(readBuffer);
            while (bytesIn != -1) {
                zos.write(readBuffer, 0, bytesIn);
                bytesIn = fis.read(readBuffer);
            }
        } finally {
            fis.close();
        }
    }
}

From source file:com.l2jfree.sql.L2DataSource.java

protected static final boolean writeBackup(String databaseName, InputStream in) throws IOException {
    FileUtils.forceMkdir(new File("backup/database"));

    final Date time = new Date();

    final L2TextBuilder tb = new L2TextBuilder();
    tb.append("backup/database/DatabaseBackup_");
    tb.append(new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()));
    tb.append("_uptime-").append(L2Config.getShortUptime());
    tb.append(".zip");

    final File backupFile = new File(tb.moveToString());

    int written = 0;
    ZipOutputStream out = null;
    try {/*from  w w w.  j a v  a 2 s. co m*/
        out = new ZipOutputStream(new FileOutputStream(backupFile));
        out.setMethod(ZipOutputStream.DEFLATED);
        out.setLevel(Deflater.BEST_COMPRESSION);
        out.setComment("L2jFree Schema Backup Utility\r\n\r\nBackup date: "
                + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS z").format(new Date()));
        out.putNextEntry(new ZipEntry(databaseName + ".sql"));

        byte[] buf = new byte[4096];
        for (int read; (read = in.read(buf)) != -1;) {
            out.write(buf, 0, read);

            written += read;
        }
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }

    if (written == 0) {
        backupFile.delete();
        return false;
    }

    _log.info("DatabaseBackupManager: Database `" + databaseName + "` backed up successfully in "
            + (System.currentTimeMillis() - time.getTime()) / 1000 + " s.");
    return true;
}