call lzma_init_encoder before each compression
This commit is contained in:
parent
abbfcd5eb8
commit
0cf1a1c18b
@ -22,44 +22,48 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
const std::filesystem::path directory = argc > 1 ? argv[1] : std::filesystem::current_path();
|
||||
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();
|
||||
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;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user