Java ZipOutputStream 写入一些文件但不写入其他文件
Java ZipOutputStream writes some files but not others
我正在使用 Java 的 ZipOutputStream class 来编写一个大的 zip 文件。
当zip文件只有1000个子文件和子文件夹时,它工作正常。
当 zip 文件只有 10000 个子文件和子文件夹时,它也能正常工作。
但是,出于某种原因,当我将其增加到超过 100000 个子文件和子文件夹时,问题开始出现。
它仍会写入一大块子文件,但随后会退出。我最终得到了一个 zip 文件,其中包含大约一半的目录树。
zip 文件没有达到 4GB 的上限。完成后,zip 文件整体只有 30MB 左右。 zip 文件中的每个子文件的大小都小于 1KB。
zip 文件的布局很简单:
根文件夹应包含十个子目录。
每个子目录应包含十个子目录。
这一直持续到第四层,每个子目录包含十个文件。
但是执行后,第一层只有5个目录,第一层最后一个目录只有两个子目录。
相关代码如下:
public class MyZipFile{
MyFolder folder;
public void create(String[] haystack){
folder = new MyFolder();
folder.create(haystack);
}
public void writeToDisk(String rootPath){
FileOutputStream rootFile;
BufferedOutputStream rootBuffer;
ZipOutputStream rootZip;
try{
rootFile = new FileOutputStream(rootPath);
rootBuffer = new BufferedOutputStream(rootFile);
rootZip = new ZipOutputStream(rootBuffer);
folder.writeToDisk(rootZip);
rootZip.close();
}
}
}
和
public class MyKeyFolder extends MyFileSystemElement {
private final int numSubfiles = 10;
private MyFileSystemElement[] subfiles;
public MyFolder(int[] catalogNumber){
super(catalogNumber);
subfiles = new MyFileSystemElement[numSubfiles];
}
@Override
public void create(String[] haystack){
for(int i=0; i < numSubfiles; i++){
MyFileSystemElement element;
int[] subCatalogNumber = createSubCatalogNumber(i);
//finalFolderLevel determines how many files are in the ZIP
//When finalFolderLevel is 2, there are about 1000 subfiles.
//When finalFolderLevel is 3, there are about 10000 subfiles.
//When finalFolderLevel is 4, there are about 100000 subfiles
//(and this is where I run into trouble).
if(thisFolderLevel <= finalFolderLevel)
element = new myFolder(subCatalogNumber);
else
element = new myFile(subCatalogNumber);
subfiles[i] = element;
subfiles[i].create(haystack);
}
}
@Override
public void writeToDisk(ZipOutputStream zipStream) throws IOException {
String path = createPathFromCatalogNumber();
zipStream.putNextEntry(new ZipEntry(path));
zipStream.flush();
zipStream.closeEntry();
for(MyFileSystemElement file : subfiles){
file.writeToDisk(zipStream);
}
}
}
和
public class MyFile extends MyFileSystemElement {
private String fileContents;
@Override
public void create(String[] haystack){
fileContents = "";
fileContents += haystack[deriveIndex(0)] + "\n";
fileContents += haystack[deriveIndex(1)] + "\n";
fileContents += haystack[deriveIndex(2)] + "\n";
fileContents += haystack[deriveIndex(3)] + "\n";
fileContents += haystack[deriveIndex(4)] + "\n";
fileContents += haystack[deriveIndex(5)] + "\n";
fileContents += haystack[deriveIndex(6)] + "\n";
fileContents += haystack[deriveIndex(7)] + "\n";
fileContents += haystack[deriveIndex(8)] + "\n";
fileContents += haystack[deriveIndex(9)] + "\n";
}
@Override
public void writeToDisk(ZipOutputStream zipStream) throws IOException {
String path = createPathFromCatalogNumber();
ZipEntry entry = new ZipEntry(path);
byte[] contents = fileContents.getBytes();
zipStream.putNextEntry(entry);
zipStream.write(contents);
zipStream.flush();
zipStream.closeEntry();
}
}
我想知道 ZipOutputStream 是否有它在退出之前可以写入的最大文件数。或者 FileOutputStream 可以。我不确定。
感谢您的帮助。谢谢。
原始 INFO-ZIP 格式(Java 实现)似乎限制为 65,536 个条目:
http://www.info-zip.org/FAQ.html#limits
这是 WinZIP 相对于其 ZIP64 格式的限制的另一个角度:
http://kb.winzip.com/kb/entry/99/
您可能想使用不同的存档格式或找出限制文件数量的方法。
我正在使用 Java 的 ZipOutputStream class 来编写一个大的 zip 文件。
当zip文件只有1000个子文件和子文件夹时,它工作正常。 当 zip 文件只有 10000 个子文件和子文件夹时,它也能正常工作。
但是,出于某种原因,当我将其增加到超过 100000 个子文件和子文件夹时,问题开始出现。
它仍会写入一大块子文件,但随后会退出。我最终得到了一个 zip 文件,其中包含大约一半的目录树。
zip 文件没有达到 4GB 的上限。完成后,zip 文件整体只有 30MB 左右。 zip 文件中的每个子文件的大小都小于 1KB。
zip 文件的布局很简单:
根文件夹应包含十个子目录。
每个子目录应包含十个子目录。
这一直持续到第四层,每个子目录包含十个文件。
但是执行后,第一层只有5个目录,第一层最后一个目录只有两个子目录。
相关代码如下:
public class MyZipFile{
MyFolder folder;
public void create(String[] haystack){
folder = new MyFolder();
folder.create(haystack);
}
public void writeToDisk(String rootPath){
FileOutputStream rootFile;
BufferedOutputStream rootBuffer;
ZipOutputStream rootZip;
try{
rootFile = new FileOutputStream(rootPath);
rootBuffer = new BufferedOutputStream(rootFile);
rootZip = new ZipOutputStream(rootBuffer);
folder.writeToDisk(rootZip);
rootZip.close();
}
}
}
和
public class MyKeyFolder extends MyFileSystemElement {
private final int numSubfiles = 10;
private MyFileSystemElement[] subfiles;
public MyFolder(int[] catalogNumber){
super(catalogNumber);
subfiles = new MyFileSystemElement[numSubfiles];
}
@Override
public void create(String[] haystack){
for(int i=0; i < numSubfiles; i++){
MyFileSystemElement element;
int[] subCatalogNumber = createSubCatalogNumber(i);
//finalFolderLevel determines how many files are in the ZIP
//When finalFolderLevel is 2, there are about 1000 subfiles.
//When finalFolderLevel is 3, there are about 10000 subfiles.
//When finalFolderLevel is 4, there are about 100000 subfiles
//(and this is where I run into trouble).
if(thisFolderLevel <= finalFolderLevel)
element = new myFolder(subCatalogNumber);
else
element = new myFile(subCatalogNumber);
subfiles[i] = element;
subfiles[i].create(haystack);
}
}
@Override
public void writeToDisk(ZipOutputStream zipStream) throws IOException {
String path = createPathFromCatalogNumber();
zipStream.putNextEntry(new ZipEntry(path));
zipStream.flush();
zipStream.closeEntry();
for(MyFileSystemElement file : subfiles){
file.writeToDisk(zipStream);
}
}
}
和
public class MyFile extends MyFileSystemElement {
private String fileContents;
@Override
public void create(String[] haystack){
fileContents = "";
fileContents += haystack[deriveIndex(0)] + "\n";
fileContents += haystack[deriveIndex(1)] + "\n";
fileContents += haystack[deriveIndex(2)] + "\n";
fileContents += haystack[deriveIndex(3)] + "\n";
fileContents += haystack[deriveIndex(4)] + "\n";
fileContents += haystack[deriveIndex(5)] + "\n";
fileContents += haystack[deriveIndex(6)] + "\n";
fileContents += haystack[deriveIndex(7)] + "\n";
fileContents += haystack[deriveIndex(8)] + "\n";
fileContents += haystack[deriveIndex(9)] + "\n";
}
@Override
public void writeToDisk(ZipOutputStream zipStream) throws IOException {
String path = createPathFromCatalogNumber();
ZipEntry entry = new ZipEntry(path);
byte[] contents = fileContents.getBytes();
zipStream.putNextEntry(entry);
zipStream.write(contents);
zipStream.flush();
zipStream.closeEntry();
}
}
我想知道 ZipOutputStream 是否有它在退出之前可以写入的最大文件数。或者 FileOutputStream 可以。我不确定。
感谢您的帮助。谢谢。
原始 INFO-ZIP 格式(Java 实现)似乎限制为 65,536 个条目:
http://www.info-zip.org/FAQ.html#limits
这是 WinZIP 相对于其 ZIP64 格式的限制的另一个角度:
http://kb.winzip.com/kb/entry/99/
您可能想使用不同的存档格式或找出限制文件数量的方法。