Java ZipEntry Add addZipEntryRecursive(ZipOutputStream zipOut, File file, String prefix)

Here you can find the source of addZipEntryRecursive(ZipOutputStream zipOut, File file, String prefix)

Description

If the file is a file, it is added to the output stream with the given prefix.

License

Open Source License

Parameter

Parameter Description
file the file or directory to add to the output stream
prefix the prefix - is either blank or ends with a forwardslash e.g. "dir/"

Exception

Parameter Description
IOException an exception

Declaration

private static void addZipEntryRecursive(ZipOutputStream zipOut, File file, String prefix) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2006, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * 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://w ww . ja va 2  s.c om
 *     The Chisel Group, University of Victoria
 *******************************************************************************/

import java.io.File;

import java.io.IOException;

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

public class Main {
    /**
     * If the file is a file, it is added to the output stream with the given prefix.
     * If the file is a directory, it calls this method.
     * @param file    the file or directory to add to the output stream
     * @param prefix the prefix - is either blank or ends with a forwardslash e.g. "dir/"
     * @throws IOException
     */
    private static void addZipEntryRecursive(ZipOutputStream zipOut, File file, String prefix) throws IOException {
        if (file.isDirectory()) {
            String[] files = file.list();
            for (String filename : files) {
                addZipEntryRecursive(zipOut, new File(file, filename), prefix + file.getName() + "/");
            }
        } else {
            zipOut.putNextEntry(new ZipEntry(prefix + file.getName()));
        }
    }
}

Related

  1. addZipEntry(String pathName, byte[] contents, ZipOutputStream zos)
  2. addZipEntry(ZipOutputStream zipOut, File file, String prefix)
  3. addZipEntry(ZipOutputStream zipOutputStream, File entryFile, String parentPath)
  4. addZipEntry(ZipOutputStream zos, File fileToZip, File baseFolder)
  5. addZipEntryFromStream(InputStream is, ZipOutputStream os, String filename)