Java Zip File zip(String outputFileName, String inputFileName)

Here you can find the source of zip(String outputFileName, String inputFileName)

Description

zip

License

Open Source License

Declaration

public static Boolean zip(String outputFileName, String inputFileName) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {

    public static Boolean zip(String outputFileName, String inputFileName) {
        ZipOutputStream out;//from   ww  w .  j  a va  2 s  . co m
        try {
            out = new ZipOutputStream(new FileOutputStream(outputFileName));
            File f = new File(inputFileName);
            if (f.exists()) {
                if (f.isDirectory()) {
                    File[] fl = f.listFiles();
                    for (File element : fl) {
                        out.putNextEntry(new ZipEntry(element.getName()));
                        FileInputStream in = new FileInputStream(element);
                        byte[] buffer = new byte[1024];
                        int b = 0;
                        while ((b = in.read(buffer)) != -1) {
                            out.write(buffer, 0, b);
                            out.flush();
                        }
                        in.close();
                    }
                } else if (f.isFile()) {
                    out.putNextEntry(new ZipEntry(f.getName()));
                    FileInputStream in = new FileInputStream(f);
                    byte[] buffer = new byte[1024];
                    int b = 0;
                    while ((b = in.read(buffer)) != -1) {
                        out.write(buffer, 0, b);
                        out.flush();
                    }
                    in.close();
                }
            } else {
                return false;
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

Related

  1. zip(OutputStream outputStream, File targetFile)
  2. zip(String data, String fileName)
  3. zip(String dir, String destFile)
  4. zip(String filesDirToBeZipped, String destFileName, String manifest)
  5. zip(String inputStr)
  6. Zip(String path, File file)
  7. zip(String path, String zipFilePath)
  8. zip(String payload)
  9. zip(String src, String des)