Java Zip Files addToZipFile(String fileName, ZipOutputStream zos)

Here you can find the source of addToZipFile(String fileName, ZipOutputStream zos)

Description

add To Zip File

License

Open Source License

Declaration

public static void addToZipFile(String fileName, ZipOutputStream zos)
            throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

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

public class Main {
    public static void addToZipFile(String fileName, ZipOutputStream zos)
            throws FileNotFoundException, IOException {

        System.out.println("Writing '" + fileName + "' to zip file");

        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        ZipEntry zipEntry = new ZipEntry(file.getName());
        zos.putNextEntry(zipEntry);/*from   w  w w . j  av a  2s . co m*/

        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }

        zos.closeEntry();
        fis.close();
    }
}

Related

  1. addToZip(String path, String srcFile, ZipOutputStream zip)
  2. addToZip(String path, String srcFile, ZipOutputStream zip)
  3. addToZip(String[] sourceFiles, ZipOutputStream output)
  4. addToZip(ZipOutputStream zos, String rootDirectoryName, String fileName)
  5. addToZipFile(InputStream source, String entryName, ZipOutputStream zos)
  6. fileToZip(File file, File zipFile)
  7. fileToZipFile(File toZip, File output)
  8. makeZip(File dir, File zipFile)
  9. makeZip(String[] inFilePaths, String zipFilePath)