Java ZipOutputStream Write zipFile(ZipOutputStream out, File file, String dir)

Here you can find the source of zipFile(ZipOutputStream out, File file, String dir)

Description

zip File

License

MIT License

Declaration

private static void zipFile(ZipOutputStream out, File file, String dir) throws IOException 

Method Source Code


//package com.java2s;
/*//from  ww  w.  ja v a  2s . co  m
 * This source code is licensed under the MIT-style license found in the
 * LICENSE file in the root directory of this source tree.
 */

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

public class Main {

    private static void zipFile(ZipOutputStream out, File file, String dir) throws IOException {
        if (file.isDirectory()) {
            out.putNextEntry(new ZipEntry(dir + "/"));
            dir = dir.length() == 0 ? "" : dir + "/";

            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                zipFile(out, files[i], dir + files[i].getName());
            }
        } else {
            FileInputStream fis = new FileInputStream(file);
            out.putNextEntry(new ZipEntry(dir));

            int tempByte;
            byte[] buffer = new byte[1024];
            while ((tempByte = fis.read(buffer)) > 0) {
                out.write(buffer, 0, tempByte);
            }

            fis.close();
        }
    }
}

Related

  1. writeFolderEntry(ZipOutputStream zout, String relpath)
  2. zip(File current, String rootPath, ZipOutputStream zipStream, byte[] buffer)
  3. zip(File[] files, String baseDir, ZipOutputStream zos)
  4. zip(final File directory, final File base, final ZipOutputStream zipOutputStream)
  5. zipAutoBase(File f, File base, ZipOutputStream out)
  6. zipFile(ZipOutputStream out, File sourceFile)
  7. zipFile(ZipOutputStream out, String stripPath, File file, char pathSeparator)
  8. zipFile(ZipOutputStream zipOut, String path, File file)
  9. zipFile(ZipOutputStream zos, BufferedOutputStream out, File destPath, String originalPath)