我正在帮助库[miniz] [1]构建一个程序来压缩大小不超过3GB的文件。运行该程序的计算机也将运行另一个(重)应用程序,因此我希望这个压缩程序加载每个文件的一大块,以防止它使用大量的RAM(最大块大小= 0.5 GB),压缩chunk然后继续下一个块,直到所有文件都被压缩。
要知道,这不能正常工作,例如:如果一个名为problem.txt的文件被分成10个块,我会在我的zip文件夹中找到10个名为problem.txt的文件。显然我希望将这些块合并在一起,而不是在zip中拆分。
这可能与miniz有关吗? 以下文本写在libary文件中(libary只包含一个文件),所以我想这是不可能的,但无论如何我要问是否有人有解决方案或其他方法,所以程序不会占用所有内存。
The ZIP archive API's where designed with simplicity and efficiency in mind, with just enough abstraction to
get the job done with minimal fuss. There are simple API's to retrieve file information, read files from
existing archives, create new archives, append new files to existing archives, or clone archive data from
one archive to another. It supports archives located in memory or the heap, on disk (using stdio.h),
or you can specify custom file read/write callbacks.
程序崩溃,文件大于0.9 GB。 [1]:https://code.google.com/p/miniz/。
请注意,程序将整个文件存储在std :: vector filesdata中。每个元素都是一大块数据。在最终版本中,只需要读取一个块并将其存储在程序中。此版本中的问题是该库在.zip中创建了许多具有相同名称的文件,如上所述 我现在错误地使用了lib吗?我自己打开文件并将数据存储在向量中,因为我无法弄清楚如何让函数打开文件本身。
for (i = 0; i < filesData.size(); ++i)
{
sprintf(data.at(i), filesData.at(i) );
sprintf(archive_filename, fileNames.at(i) );
// Add a new file to the archive. Note this is an IN-PLACE operation, so if it fails your archive is probably hosed (its central directory may not be complete) but it should be recoverable using zip -F or -FF. So use caution with this guy.
// A more robust way to add a file to an archive would be to read it into memory, perform the operation, then write a new archive out to a temp file and then delete/rename the files.
// Or, write a new archive to disk to a temp file, then delete/rename the files. For this test this API is fine.
status = mz_zip_add_mem_to_archive_file_in_place(s_Test_archive_filename, archive_filename, data.at(i), strlen(data.at(i)) + 1, s_pComment, (uint16)strlen(s_pComment), MZ_BEST_COMPRESSION);
if (!status)
{
printf("mz_zip_add_mem_to_archive_file_in_place failed! 2\n");
return EXIT_FAILURE;
}
}
我做了一个小测试程序(失败了)。
#include "miniz.c"
#if defined(__GNUC__)
// Ensure we get the 64-bit variants of the CRT's file I/O calls
#ifndef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64
#endif
#ifndef _LARGEFILE64_SOURCE
#define _LARGEFILE64_SOURCE 1
#endif
#endif
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint;
int main(int argc, char *argv[])
{
mz_zip_archive zip_archive;
const char *s_Test_archive_filename = "__mz_example2_test__.zip";
const char *s_pComment = "This is a comment";
remove(s_Test_archive_filename);
printf (argv[1] );
bool status = mz_zip_writer_add_file( (&zip_archive), s_Test_archive_filename, argv[1], s_pComment, (uint16)strlen(s_pComment), MZ_BEST_COMPRESSION );
if (!status)
{
printf("mz_zip_reader_init_file() failed!\n");
return EXIT_FAILURE;
}
else
{
printf("success\n");
}
return 0;
}