template
void
Gzip (const string& pathFileIn,
const string& pathFileOut,
FunctionInit_ FunctionInit,
int Function (::z_streamp, int),
int FunctionEnd (::z_streamp),
Args&&... args)
{
std::fstream fileIn(pathFileIn, std::ios::in | std::ios::binary),
fileOut(pathFileOut, std::ios::out | std::ios::binary);
::z_stream stream = {}; // make all values set to 0
VERIFY(FunctionInit(&stream, STD_FORWARD(args), ZLIB_VERSION, sizeof(z_stream)) == Z_OK,
"Initialization failed for input file: " + pathFileIn,);
LOG_D(Function == &inflate? "Un" : "", "Compression started for file: ", pathFileIn,
" with size(bytes): ", std::filesystem::file_size(pathFileIn));
for(int flush = Z_NO_FLUSH; flush == Z_NO_FLUSH; stream.avail_out = 0)
{
string input(MEGABYTE_t, 0);
fileIn.read(&input[0], MEGABYTE_t);
stream.avail_in = fileIn.gcount();
if(stream.avail_in != input.size())
{
input.resize(stream.avail_in); // set to actual bytes read
flush = Z_FINISH;
}
stream.next_in = reinterpret_cast(&input[0]);
while(stream.avail_out == 0)
{
string output(stream.avail_out = 10 * MEGABYTE_t, 0); // for uncompression, output is larger
stream.next_out = reinterpret_cast(&output[0]);
const auto totalOutLast = stream.total_out;
if(Function(&stream, flush) == Z_MEM_ERROR)
{
LOG_E("===CRITICAL=== Not enough memory while GZip for file: ", pathFileIn);
return;
}
output.resize(stream.total_out - totalOutLast);
(fileOut
Gzip называется как: < /p>
void
FileToGzip (const string& pathFileIn,
const string& pathFileOut,
const int level /*= Z_DEFAULT_COMPRESSION*/)
{
Gzip(pathFileIn, pathFileOut, ::deflateInit2_, ::deflate, ::deflateEnd,
level, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY);
}
< /code>
и unzip называется (не имеет отношения к этому вопросу): < /p>
void
GzipToFile (const string& pathFileIn,
const string& pathFileOut)
{
Gzip(pathFileIn, pathFileOut, ::inflateInit2_, ::inflate, ::inflateEnd, 15 | 16);
}
< /code>
Тем не менее, я заметил, что иногда мой код сбивается с приведенной ниже строки: < /p>
if(Function(&stream, flush) == Z_MEM_ERROR) // Task Manager shows 95+% RAM usage upon crash
Странно, stream.avail_in = 276672 Результаты в потоке.total_out = 98917358 в операции сжатия! Как это возможно? Есть ли что -то возможное, что я могу улучшить или пропустить в алгоритме, чтобы избежать аварии? Я не могу изменить баран.
[code]template void Gzip (const string& pathFileIn, const string& pathFileOut, FunctionInit_ FunctionInit, int Function (::z_streamp, int), int FunctionEnd (::z_streamp), Args&&... args) { std::fstream fileIn(pathFileIn, std::ios::in | std::ios::binary), fileOut(pathFileOut, std::ios::out | std::ios::binary); ::z_stream stream = {}; // make all values set to 0 VERIFY(FunctionInit(&stream, STD_FORWARD(args), ZLIB_VERSION, sizeof(z_stream)) == Z_OK, "Initialization failed for input file: " + pathFileIn,);
LOG_D(Function == &inflate? "Un" : "", "Compression started for file: ", pathFileIn, " with size(bytes): ", std::filesystem::file_size(pathFileIn)); for(int flush = Z_NO_FLUSH; flush == Z_NO_FLUSH; stream.avail_out = 0) { string input(MEGABYTE_t, 0); fileIn.read(&input[0], MEGABYTE_t); stream.avail_in = fileIn.gcount(); if(stream.avail_in != input.size()) { input.resize(stream.avail_in); // set to actual bytes read flush = Z_FINISH; } stream.next_in = reinterpret_cast(&input[0]);
while(stream.avail_out == 0) { string output(stream.avail_out = 10 * MEGABYTE_t, 0); // for uncompression, output is larger stream.next_out = reinterpret_cast(&output[0]); const auto totalOutLast = stream.total_out; if(Function(&stream, flush) == Z_MEM_ERROR) { LOG_E("===CRITICAL=== Not enough memory while GZip for file: ", pathFileIn); return; } output.resize(stream.total_out - totalOutLast); (fileOut Gzip называется как: < /p> void FileToGzip (const string& pathFileIn, const string& pathFileOut, const int level /*= Z_DEFAULT_COMPRESSION*/) { Gzip(pathFileIn, pathFileOut, ::deflateInit2_, ::deflate, ::deflateEnd, level, Z_DEFLATED, 15 | 16, 8, Z_DEFAULT_STRATEGY); } < /code> и unzip называется (не имеет отношения к этому вопросу): < /p> void GzipToFile (const string& pathFileIn, const string& pathFileOut) { Gzip(pathFileIn, pathFileOut, ::inflateInit2_, ::inflate, ::inflateEnd, 15 | 16); } < /code> Тем не менее, я заметил, что иногда мой код сбивается с приведенной ниже строки: < /p> if(Function(&stream, flush) == Z_MEM_ERROR) // Task Manager shows 95+% RAM usage upon crash [/code] Странно, stream.avail_in = 276672 Результаты в потоке.total_out = 98917358 в операции сжатия! Как это возможно? Есть ли что -то возможное, что я могу улучшить или пропустить в алгоритме, чтобы избежать аварии? Я не могу изменить баран.
case WriterStart(fileName,actor) =>
{
this.filename = fileName
this.actor = actor
zip = new GZIPOutputStream(new FileOutputStream(new File(fileName)))
writer = new BufferedWriter(new...
Потоковая передача меня сбивает с толку: как только я думаю, что уловил идею, мой код рушится.
Я пытаюсь принять строку, gzip сжать его и сохранить в базе данных PostGreSQL. ТОГДА я хочу получить его обратно и повторно преобразовать в выходной...
Я работаю над приложением Spring Boot, в котором мне нужно загрузить файлы журналов из фиктивной службы и сохранить их как файлы GZIP в системе Windows. Однако я столкнулся с проблемой, когда сохраненный файл .gz содержит внутри себя другой файл...