Java Zip File zipFile(final File target, final ZipOutputStream zip, final File file, final String path)

Here you can find the source of zipFile(final File target, final ZipOutputStream zip, final File file, final String path)

Description

zip File

License

Open Source License

Declaration

private static void zipFile(final File target, final ZipOutputStream zip, final File file, final String path)

            throws IOException

    

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2008 Oracle//from   w  ww  .  j  a v a  2s.  c om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Konstantin Komissarchik - initial implementation and ongoing maintenance
 ******************************************************************************/

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(final File target, final ZipOutputStream zip, final File file, final String path)

            throws IOException

    {
        if (!file.equals(target)) {
            final ZipEntry ze = new ZipEntry(path);

            ze.setTime(file.lastModified() + 1999);
            ze.setMethod(ZipEntry.DEFLATED);

            zip.putNextEntry(ze);

            final FileInputStream in = new FileInputStream(file);

            try {
                int bufsize = 8 * 1024;
                final long flength = file.length();

                if (flength == 0) {
                    return;
                } else if (flength < bufsize) {
                    bufsize = (int) flength;
                }

                final byte[] buffer = new byte[bufsize];
                int count = in.read(buffer);

                while (count != -1) {
                    zip.write(buffer, 0, count);
                    count = in.read(buffer);
                }
            } finally {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

Related

  1. zipFile(File source, File target)
  2. zipFile(File source, String basePath, ZipOutputStream zos)
  3. zipFile(File sourceFile)
  4. zipFile(File zipfile, ZipOutputStream zos, String name)
  5. zipFile(final File source)
  6. zipFile(String filedir, String zippath)
  7. zipFile(String filePath, int iBaseFolderLength, ZipOutputStream jos, CRC32 crc)
  8. zipFile(String filePath, String fileName)
  9. zipFile(String inPath, String outPath, String nameInsideZip)