Java Zip File zipFile(String filePath, String fileName)

Here you can find the source of zipFile(String filePath, String fileName)

Description

zip File

License

Apache License

Declaration

public static void zipFile(String filePath, String fileName) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static void zipFile(String filePath, String fileName) throws Exception {
        String[] filenames = new String[] { fileName };
        byte[] buf = new byte[1024];
        String outFilename = fileName + ".zip";
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(filePath + "/" + outFilename));

        for (int i = 0; i < filenames.length; ++i) {
            FileInputStream in = new FileInputStream(filePath + "/" + filenames[i]);
            out.putNextEntry(new ZipEntry(filenames[i]));
            boolean len = false;

            int var9;
            while ((var9 = in.read(buf)) > 0) {
                out.write(buf, 0, var9);
            }/*ww  w  .  j a va  2  s . c o  m*/

            out.closeEntry();
            in.close();
        }

        out.close();
    }
}

Related

  1. zipFile(File zipfile, ZipOutputStream zos, String name)
  2. zipFile(final File source)
  3. zipFile(final File target, final ZipOutputStream zip, final File file, final String path)
  4. zipFile(String filedir, String zippath)
  5. zipFile(String filePath, int iBaseFolderLength, ZipOutputStream jos, CRC32 crc)
  6. zipFile(String inPath, String outPath, String nameInsideZip)