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,44 +22,48 @@ 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();
if (!entry.is_regular_file() || !stringutils::has_any_ending(stringutils::to_lower(path), ENDINGS))
{ {
const std::string path = entry.path().string(); continue;
if (entry.is_regular_file() && stringutils::has_any_ending(stringutils::to_lower(path), ENDINGS))
{
std::cout << "Compressing " + path + "..." << std::flush;
const std::string output_path = path + ".xz";
FILE *infile = std::fopen(path.c_str(), "rb");
FILE *outfile = std::fopen(output_path.c_str(), "wb");
success = lzma_compress(&strm, infile, outfile);
std::fclose(infile);
if (std::fclose(outfile))
{
fprintf(stderr, "Write error: %s\n", std::strerror(errno));
success = false;
}
if (!success) {
std::cout << " FAILED!" << std::endl
<< std::flush;
break;
}
std::filesystem::remove(path);
std::cout << " OK!" << std::endl
<< 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";
FILE *infile = std::fopen(path.c_str(), "rb");
FILE *outfile = std::fopen(output_path.c_str(), "wb");
success = lzma_compress(&strm, infile, outfile);
std::fclose(infile);
if (std::fclose(outfile))
{
fprintf(stderr, "Write error: %s\n", std::strerror(errno));
success = false;
}
if (!success) {
std::cout << " FAILED!" << std::endl;
break;
}
std::filesystem::remove(path);
std::cout << " OK!" << std::endl;
} }
lzma_end(&strm); lzma_end(&strm);