zip File - Java File Path IO

Java examples for File Path IO:Zip File

Description

zip File

Demo Code


//package com.java2s;
import java.io.*;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    public static void zipFile(File inputFile, File outputZip) {
        byte[] buffer = new byte[1024];

        try {/*from w  w w. j a va 2s  . c om*/
            FileOutputStream fos = new FileOutputStream(outputZip);
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze = new ZipEntry(inputFile.getName());
            zos.putNextEntry(ze);
            FileInputStream in = new FileInputStream(inputFile);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            in.close();
            zos.closeEntry();

            zos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials