Here you can find the source of zip(List
Parameter | Description |
---|---|
fileNames | list of file names to zip |
fileContents | list of file contents |
Parameter | Description |
---|
public static byte[] zip(List<String> fileNames, List<byte[]> fileContents) throws IOException
//package com.java2s; /*/* w w w . ja va2s . co m*/ * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 * * http://www.apache.org/licenses/LICENSE-2.0 * * 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.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ByteArrayOutputStream; import java.util.List; import java.util.Map; import java.util.zip.ZipOutputStream; import java.util.zip.ZipEntry; public class Main { /** Zip files * * @param fileNames list of file names to zip * @param fileContents list of file contents * @return zip byte content * @throws java.io.IOException IOException */ public static byte[] zip(List<String> fileNames, List<byte[]> fileContents) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create the ZIP file ZipOutputStream out = new ZipOutputStream(baos); for (int i = 0, size = fileNames.size(); i < size; i++) { String name = fileNames.get(i); byte[] content = fileContents.get(i); // Add ZIP entry to output stream. ZipEntry entry = new ZipEntry(name); entry.setSize(content.length); out.putNextEntry(entry); out.write(content); // Complete the entry out.closeEntry(); } out.close(); return baos.toByteArray(); } /** Zip files * * @param namesMap map of files to zip , each entry is the name of the folder where these files are added * @param contentsMap map of file contents * @return zip byte content * @throws java.io.IOException IOException */ public static byte[] zip(Map<String, List<String>> namesMap, Map<String, List<byte[]>> contentsMap) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create the ZIP file ZipOutputStream out = new ZipOutputStream(baos); for (String folder : namesMap.keySet()) { // create the folder out.putNextEntry(new ZipEntry(folder + "/")); List<String> fileNames = namesMap.get(folder); List<byte[]> fileContents = contentsMap.get(folder); for (int i = 0, size = fileNames.size(); i < size; i++) { String name = fileNames.get(i); byte[] content = fileContents.get(i); // Add ZIP entry to output stream. ZipEntry entry = new ZipEntry(folder + "/" + name); entry.setSize(content.length); out.putNextEntry(entry); out.write(content); // Complete the entry out.closeEntry(); } } out.close(); return baos.toByteArray(); } public static void zip(List<File> files, String outFileName, String withoutBase) { // Create a buffer for reading the files byte[] buf = new byte[1024]; try { // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFileName)); // Compress the files for (File file : files) { if (file.isDirectory()) { continue; } FileInputStream in = new FileInputStream(file); // Add ZIP entry to output stream. String fileName = file.getAbsolutePath(); if (withoutBase != null) { fileName = fileName.substring(withoutBase.length() + 1); } out.putNextEntry(new ZipEntry(fileName)); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); } catch (IOException e) { e.printStackTrace(); } } }