Java Zip Folder doZip(File fileIn, String fileOut)

Here you can find the source of doZip(File fileIn, String fileOut)

Description

do Zip

License

Open Source License

Declaration

public static File doZip(File fileIn, String fileOut) throws Exception 

Method Source Code


//package com.java2s;
/*// ww w  .  jav  a  2  s  . c o  m
 * Copyright (c) 2007-2016 AREasy Runtime
 *
 * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software");
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT,
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 */

import java.io.*;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    public static File doZip(File fileIn, String fileOut) throws Exception {
        if (fileIn == null || !fileIn.exists())
            return null;

        //Create the ZIP file
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(fileOut));
        FileInputStream in = new FileInputStream(fileIn);

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

        // Transfer bytes from the file to the ZIP file
        int len;
        byte[] buf = new byte[1024];

        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }

        // Complete the entry
        out.closeEntry();
        in.close();

        // Complete the ZIP file
        out.close();

        return new File(fileOut);
    }

    public static File doZip(File file) throws Exception {
        if (file == null || !file.exists())
            return null;

        String fileExt = "";
        String filePath = "";
        String fileBase = "";
        String fileName = file.getPath();

        //Get file name
        int index = file.getPath().lastIndexOf(File.separator);
        if (index > 0) {
            fileBase = file.getPath().substring(index + 1);
            filePath = file.getPath().substring(0, index);
        }

        index = fileBase.lastIndexOf(".");
        if (index > 0) {
            fileName = fileBase.substring(0, index);
            fileExt = fileBase.substring(index + 1);
        }

        //Create the ZIP file
        String target = filePath + File.separator + fileName + ".zip";

        //execute zip and return the answer
        return doZip(file, target);
    }
}

Related

  1. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  2. addFolderToZip(String path, String srcFolder, ZipOutputStream zip)
  3. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean addFolder)
  4. addFolderToZip(String path, String srcFolder, ZipOutputStream zip, boolean includeFullPath)
  5. addFolderToZip(String pathInsideZip, final File folderToZip, final ZipOutputStream outZip)
  6. doZip(Map zipStructure, File zipFile)
  7. doZip(Properties properties, String name, File f)
  8. doZip(String baseDir, String fileName)
  9. doZip(String filename, String zipFileName)