Java Zip File List zip(List srcFiles, OutputStream os)

Here you can find the source of zip(List srcFiles, OutputStream os)

Description

zip

License

Apache License

Declaration

public static ZipOutputStream zip(List<String> srcFiles, OutputStream os) throws IOException 

Method Source Code

//package com.java2s;
/**//  www . j  ava2s.  co  m
 * Copyright 2016 Milinda Pathirage
 * <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.*;

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

public class Main {
    public static ZipOutputStream zip(List<String> srcFiles, OutputStream os) throws IOException {
        // create byte buffer
        byte[] buffer = new byte[1024];
        ZipOutputStream zos = new ZipOutputStream(os);

        for (String f : srcFiles) {
            File srcFile = new File(f);
            FileInputStream fis = new FileInputStream(srcFile);
            // begin writing a new ZIP entry, positions the stream to the start of the entry data
            zos.putNextEntry(new ZipEntry(srcFile.getName()));
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }
            zos.closeEntry();
            // close the InputStream
            fis.close();
        }

        return zos;
    }
}

Related

  1. zip(File zipFile, List files)
  2. zip(Iterable files, String baseFolderName, String toZipFile)
  3. zip(List runtimeLibFiles, File saturnContainerDir, File zipFile)
  4. zip(List fileNames, List fileContents)
  5. zip(List fileNames, String outFileName)
  6. zip(String[] filename, String outname)
  7. zip(String[] sFl, String sZip)
  8. zip(String[] sourceFiles, String zipFile, String directory)
  9. zip(String[] srcFiles, String dstFile, String comment)