Java Zip File zipFile(File fileToZip, String fileName, ZipOutputStream zipOut)

Here you can find the source of zipFile(File fileToZip, String fileName, ZipOutputStream zipOut)

Description

zip File

License

Apache License

Declaration

private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException 

Method Source Code


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

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

import java.io.IOException;
import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {

        if (fileToZip.isHidden()) {
            return;
        }//from w  w w .j a  v a 2s.  co  m
        if (fileToZip.isDirectory()) {
            File[] children = fileToZip.listFiles();
            for (File childFile : children) {
                zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
            }
            return;
        }
        FileInputStream fis = new FileInputStream(fileToZip);
        ZipEntry zipEntry = new ZipEntry(fileName);
        zipOut.putNextEntry(zipEntry);
        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zipOut.write(bytes, 0, length);
        }
        fis.close();
    }
}

Related

  1. zipFile(File aFileToZip)
  2. zipFile(File file)
  3. zipFile(File file, File output)
  4. zipFile(File file, File zipFile)
  5. zipFile(File file, String zipFile)
  6. zipFile(File input, File output)
  7. zipFile(File inputFile, File outputZip)
  8. zipFile(File inputFile, String zipFilePath)
  9. zipFile(File resFile, ZipOutputStream zipout, String rootpath)