Here you can find the source of addFileToZip(File file, ZipOutputStream zos)
Parameter | Description |
---|---|
file | the file to be added |
zos | the current zip output stream |
Parameter | Description |
---|---|
FileNotFoundException | an exception |
IOException | an exception |
private static void addFileToZip(File file, ZipOutputStream zos) throws FileNotFoundException, IOException
//package com.java2s; /**// w w w. j a v a 2 s . c om * Copyright 2016 SmartBear Software * <p> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.BufferedInputStream; 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; public class Main { /** * A constants for buffer size used to read/write data */ private static final int BUFFER_SIZE = 4096; /** * Adds a file to the current zip output stream * @param file the file to be added * @param zos the current zip output stream * @throws FileNotFoundException * @throws IOException */ private static void addFileToZip(File file, ZipOutputStream zos) throws FileNotFoundException, IOException { zos.putNextEntry(new ZipEntry(file.getName())); BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file)); byte[] bytesIn = new byte[BUFFER_SIZE]; int read = 0; while ((read = bis.read(bytesIn)) != -1) { zos.write(bytesIn, 0, read); } zos.closeEntry(); } }