Java Zip File zipFile(File zipfile, ZipOutputStream zos, String name)

Here you can find the source of zipFile(File zipfile, ZipOutputStream zos, String name)

Description

zip File

License

Open Source License

Declaration

static void zipFile(File zipfile, ZipOutputStream zos, String name) throws IOException 

Method Source Code


//package com.java2s;
/*//from ww  w .  j  ava2s .c o m
 * Copyright (C) 2011 Peransin Nicolas.
 * Use is subject to license terms.
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static final int BUFFER_SIZE = 2156;
    public static final boolean DEBUG = false;

    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 {
            // 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();
        }
    }
}

Related

  1. zipFile(File source, File dest)
  2. zipFile(File source, File target)
  3. zipFile(File source, File target)
  4. zipFile(File source, String basePath, ZipOutputStream zos)
  5. zipFile(File sourceFile)
  6. zipFile(final File source)
  7. zipFile(final File target, final ZipOutputStream zip, final File file, final String path)
  8. zipFile(String filedir, String zippath)
  9. zipFile(String filePath, int iBaseFolderLength, ZipOutputStream jos, CRC32 crc)