集册 Java实例教程 将目录添加到当前的zip输出流

将目录添加到当前的zip输出流

欢马劈雪     最近更新时间:2020-01-02 10:19:05

559
将目录添加到当前的zip输出流


//package com.nowjava;

import java.io.BufferedInputStream;
/* 
*来 自
 n  o  w  j  a  v  a . c o m
*/


import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;


import java.io.IOException;


import java.util.zip.ZipEntry;


import java.util.zip.ZipOutputStream;
/*
nowjava.com 提供
*/


public class Main {

    /**

     * Size of the buffer to read/write data

     */

    private static final int BUFFER_SIZE = 4096;


    /**

     * Adds a directory to the current zip output stream

     * 

     * @param folder

     *            the directory to be added

     * @param parentFolder

     *            the path of parent directory

     * @param zos

     *            the current zip output stream

     * @throws FileNotFoundException

     * @throws IOException

     */

    private static void zipDirectory(File folder, String parentFolder,

            ZipOutputStream zos) throws FileNotFoundException, IOException {

        for (File file : folder.listFiles()) {

            if (file.isDirectory()) {

                zipDirectory(file, parentFolder + "/" + file.getName(), zos);

                continue;

            }

            zos.putNextEntry(new ZipEntry(parentFolder + "/"

                    + file.getName()));

            BufferedInputStream bis = null;

            try {

                bis = new BufferedInputStream(new FileInputStream(file));

                long bytesRead = 0;

                byte[] bytesIn = new 
展开阅读全文