call lzma_init_encoder before each compression

This commit is contained in:
Bence Pőcze 2022-02-21 11:44:28 +01:00
parent abbfcd5eb8
commit 0cf1a1c18b
Signed by: bence
GPG Key ID: 0520C81918569406

View File

@ -22,17 +22,25 @@ int main(int argc, char *argv[])
{ {
const std::filesystem::path directory = argc > 1 ? argv[1] : std::filesystem::current_path(); const std::filesystem::path directory = argc > 1 ? argv[1] : std::filesystem::current_path();
lzma_stream strm = LZMA_STREAM_INIT; lzma_stream strm = LZMA_STREAM_INIT;
bool success = lzma_init_encoder(&strm, COMPRESSION_PRESET); bool success = true;
if (success)
{
for (const auto &entry : std::filesystem::directory_iterator(directory)) for (const auto &entry : std::filesystem::directory_iterator(directory))
{ {
const std::string path = entry.path().string(); const std::string path = entry.path().string();
if (entry.is_regular_file() && stringutils::has_any_ending(stringutils::to_lower(path), ENDINGS))
if (!entry.is_regular_file() || !stringutils::has_any_ending(stringutils::to_lower(path), ENDINGS))
{ {
continue;
}
std::cout << "Compressing " + path + "..." << std::flush; std::cout << "Compressing " + path + "..." << std::flush;
if (!lzma_init_encoder(&strm, COMPRESSION_PRESET))
{
success = false;
break;
}
const std::string output_path = path + ".xz"; const std::string output_path = path + ".xz";
FILE *infile = std::fopen(path.c_str(), "rb"); FILE *infile = std::fopen(path.c_str(), "rb");
@ -49,17 +57,13 @@ int main(int argc, char *argv[])
} }
if (!success) { if (!success) {
std::cout << " FAILED!" << std::endl std::cout << " FAILED!" << std::endl;
<< std::flush;
break; break;
} }
std::filesystem::remove(path); std::filesystem::remove(path);
std::cout << " OK!" << std::endl std::cout << " OK!" << std::endl;
<< std::flush;
}
}
} }
lzma_end(&strm); lzma_end(&strm);