Java Zip Files addFileToZip(File root, File file, ZipOutputStream zos)

Here you can find the source of addFileToZip(File root, File file, ZipOutputStream zos)

Description

add File To Zip

License

Apache License

Declaration

private static void addFileToZip(File root, File file, ZipOutputStream zos) 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 addFileToZip(File root, File file, ZipOutputStream zos) throws IOException {
        FileInputStream fis = new FileInputStream(file);

        String filePath = file.getCanonicalPath();
        String zipFilePath = filePath.substring(root.getCanonicalPath().length() + 1, filePath.length());
        ZipEntry zipEntry = new ZipEntry(zipFilePath);
        zos.putNextEntry(zipEntry);/*from w w  w .  j ava2  s.com*/

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

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

Related

  1. addFileToZip(File file, String entryName, ZipOutputStream zos)
  2. addFileToZip(File file, String entryName, ZipOutputStream zOut)
  3. addFileToZip(File file, String parentFolderName, ZipOutputStream zip)
  4. addFileToZip(File file, ZipOutputStream zos)
  5. addFileToZip(File in, File parent, ZipOutputStream out)
  6. addFileToZip(final File file, final String zipName, final ZipOutputStream zipout)
  7. addFileToZip(final String path, final String srcFile, final ZipOutputStream zip, boolean flag)
  8. addFileToZip(final String pathInsideZip, final File fileToZip, final ZipOutputStream outZip)
  9. addFileToZip(int skipprefix, File file, ZipOutputStream zipper)