Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- net.lingala.zip4j.core.ZipFile zipFile = new ZipFile("out.zip");
- ZipParameters parameters = new ZipParameters();
- parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
- parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
- zipFile.createZipFileFromFolder("path/to/source/dir", parameters, true, maximum size);
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.zip.ZipEntry;
- import java.util.zip.ZipOutputStream;
- public class QDE_ZIP {
- public static String createZIP(String directoryPath, String zipFileName, String filesToZip) {
- try {
- final int BUFFER = 104857600; // 100MB
- final long MAX_ZIP_SIZE = 3221225472L; //3 GB
- long currentSize = 0;
- int zipSplitCount =0;
- String files[] = filesToZip.split(",");
- if (!directoryPath.endsWith("/")) {
- directoryPath = directoryPath + "/";
- }
- byte fileRAW[] = new byte[BUFFER];
- ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(directoryPath + zipFileName.toUpperCase()));
- ZipEntry zipEntry;
- FileInputStream entryFile;
- for (String aFile : files) {
- zipEntry = new ZipEntry(aFile);
- if (currentSize >= MAX_ZIP_SIZE)
- {
- zipSplitCount ++;
- //zipOut.closeEntry();
- zipOut.close();
- zipOut = new ZipOutputStream(new FileOutputStream(directoryPath + zipFileName.toLowerCase().replace(".zip", "_"+zipSplitCount+".zip").toUpperCase()));
- currentSize = 0;
- }
- zipOut.putNextEntry(zipEntry);
- entryFile = new FileInputStream(directoryPath + aFile);
- int count;
- while ((count = entryFile.read(fileRAW, 0, BUFFER)) != -1) {
- zipOut.write(fileRAW, 0, count);
- //System.out.println("number of Bytes read = " + count);
- }
- entryFile.close();
- zipOut.closeEntry();
- currentSize += zipEntry.getCompressedSize();
- }
- zipOut.close();
- //System.out.println(directory + " -" + zipFileName + " -Number of Files = " + files.length);
- } catch (FileNotFoundException e) {
- return "FileNotFoundException = " + e.getMessage();
- } catch (IOException e) {
- return "IOException = " + e.getMessage();
- } catch (Exception e) {
- return "Exception = " + e.getMessage();
- }
- return "1";
- }
- }
- import java.util.zip.*;
- import java.io.*;
- public class ChunkedZipTwo {
- static final long MAX_LIMIT=10 * 1000 * 1024; //10MB limit - hopefully this
- public static void main(String[] args) throws Exception {
- String[] files = {"file1", "file2", "file3"};
- int i = 0;
- boolean needNewFile = false;
- long overallSize = 0;
- ZipOutputStream out = getOutputStream(i);
- byte[] buffer = new byte[1024];
- for (String thisFileName: files) {
- if (overallSize > MAX_LIMIT) {
- out.close();
- i++;
- out = getOutputStream(i);
- overallSize=0;
- }
- FileInputStream in = new FileInputStream(thisFileName);
- ZipEntry ze = new ZipEntry(thisFileName);
- out.putNextEntry(ze);
- int len;
- while ((len = in.read(buffer)) > 0) {
- out.write(buffer, 0, len);
- }
- out.closeEntry();
- in.close();
- overallSize+=ze.getCompressedSize();
- }
- out.close();
- }
- public static ZipOutputStream getOutputStream(int i) throws IOException {
- ZipOutputStream out = new ZipOutputStream(new FileOutputStream("bigfile" + i +".zip"));
- out.setLevel(Deflater.DEFAULT_COMPRESSION);
- return out;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement