我正在使用下面的代碼/類來分割和壓縮大量/大小的文件。 我已經測試這個類上下面
- 數目的未壓縮的文件:116
- 總大小(未壓縮):29.1 GB
- ZIP文件大小限制(各):3 GB [MAX_ZIP_SIZE]
- 總大小(壓縮):7.85 GB(splited爲MAX_ZIP_SIZE)
- 數ZIP文件的:3
你必須的MAX_ZIP_SIZE值更改爲16(MB)* 1024 * 1024 = 16777216-22(拉鍊頭的大小)= 。
在我的代碼中,MAX_ZIP_SIZE設置爲3 GB(ZIP has limitation of 4GB on various things)。
final long MAX_ZIP_SIZE = 3221225472L; // 3 GB
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";
}
}
我回來了所有異常消息作爲字符串來使用它。這個 我自己的案例與項目有關。
你從網上試過的一些例子是什麼?當你嘗試他們時發生了什麼? –
http://stackoverflow.com/questions/11084823/filenotfoundexception-no-such-file-or-directory這是我的第一篇文章,有一個錯誤,我現在修復了,但現在2更多的出現....第一個是我似乎無法設置ZipEntry的大小,第二個是,當我嘗試提取文件時,我得到的錯誤'不支持壓縮方法' – androidu
也因爲我無法設置ZipEntry的大小我的邏輯不起作用,因此它總是隻創建一個存檔 – androidu