Java Zip File zipFile(File file, String zipFile)

Here you can find the source of zipFile(File file, String zipFile)

Description

zip File

License

Open Source License

Declaration

public static void zipFile(File file, String zipFile) throws Exception 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Chen Chao(cnfree2000@hotmail.com).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w  w w  .  j  av a 2 s  . c  o  m*/
 *  Chen Chao  - initial API and implementation
 *******************************************************************************/

import java.io.BufferedInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipFile(File file, String zipFile) throws Exception {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
                zipFile));
        ZipEntry ze = null;
        byte[] buf = new byte[1024];
        int readLen = 0;
        ze = new ZipEntry(file.getName());
        ze.setSize(file.length());
        ze.setTime(file.lastModified());
        zos.putNextEntry(ze);
        InputStream is = new BufferedInputStream(new FileInputStream(file));
        while ((readLen = is.read(buf, 0, 1024)) != -1) {
            zos.write(buf, 0, readLen);
        }
        is.close();
        zos.close();
    }
}

Related

  1. zip(String zipPath, Map input)
  2. zipFile(File aFileToZip)
  3. zipFile(File file)
  4. zipFile(File file, File output)
  5. zipFile(File file, File zipFile)
  6. zipFile(File fileToZip, String fileName, ZipOutputStream zipOut)
  7. zipFile(File input, File output)
  8. zipFile(File inputFile, File outputZip)
  9. zipFile(File inputFile, String zipFilePath)