Java ZipOutputStream Write archiveFile(String relativePath, ZipOutputStream zos, File root)

Here you can find the source of archiveFile(String relativePath, ZipOutputStream zos, File root)

Description

archive File

License

Open Source License

Declaration

public static void archiveFile(String relativePath, ZipOutputStream zos, File root) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Licensed Materials - Property of IBM/* w  ww.j  a v a  2  s  .c o  m*/
 * ? Copyright IBM Corporation 2015. All Rights Reserved.
 * 
 * Note to U.S. Government Users Restricted Rights:
 * Use, duplication or disclosure restricted by GSA ADP Schedule
 * Contract with IBM Corp. 
 *******************************************************************************/

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 {
    public static void archiveFile(String relativePath, ZipOutputStream zos, File root) throws IOException {
        byte[] buffer = new byte[1024];

        File[] files = root.listFiles();
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            String path = null;
            if (relativePath == null) {
                path = f.getName();
            } else {
                path = relativePath + "/" + f.getName(); //$NON-NLS-1$
            }
            if (f.isDirectory()) {
                archiveFile(path, zos, f);
            } else {
                ZipEntry entry = new ZipEntry(path);
                zos.putNextEntry(entry);
                FileInputStream in = null;

                try {
                    in = new FileInputStream(f);
                    int c = -1;
                    while ((c = in.read(buffer)) != -1) {
                        zos.write(buffer, 0, c);
                    }
                    zos.closeEntry();
                } finally {
                    if (in != null) {
                        in.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)