void go() override { const std::string comp_type = get_arg("type"); const size_t buf_size = get_arg_sz("buf-size"); const size_t comp_level = get_arg_sz("level"); std::unique_ptr<Botan::Compression_Algorithm> compress; compress.reset(Botan::make_compressor(comp_type)); if(!compress) { throw CLI_Error_Unsupported("Compression", comp_type); } const std::string in_file = get_arg("file"); std::ifstream in(in_file, std::ios::binary); if(!in.good()) { throw CLI_IO_Error("reading", in_file); } const std::string out_file = output_filename(in_file, comp_type); std::ofstream out(out_file, std::ios::binary); if(!in.good()) { throw CLI_IO_Error("writing", out_file); } Botan::secure_vector<uint8_t> buf; compress->start(comp_level); while(in.good()) { buf.resize(buf_size); in.read(reinterpret_cast<char*>(&buf[0]), buf.size()); buf.resize(in.gcount()); compress->update(buf); out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); } buf.clear(); compress->finish(buf); out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); out.close(); }
void go() override { const size_t buf_size = get_arg_sz("buf-size"); const std::string in_file = get_arg("file"); std::string out_file, suffix; parse_extension(in_file, out_file, suffix); std::ifstream in(in_file, std::ios::binary); if(!in.good()) { throw CLI_IO_Error("reading", in_file); } std::unique_ptr<Botan::Decompression_Algorithm> decompress; decompress.reset(Botan::make_decompressor(suffix)); if(!decompress) { throw CLI_Error_Unsupported("Decompression", suffix); } std::ofstream out(out_file, std::ios::binary); if(!out.good()) { throw CLI_IO_Error("writing", out_file); } Botan::secure_vector<uint8_t> buf; decompress->start(); while(in.good()) { buf.resize(buf_size); in.read(reinterpret_cast<char*>(&buf[0]), buf.size()); buf.resize(in.gcount()); decompress->update(buf); out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); } buf.clear(); decompress->finish(buf); out.write(reinterpret_cast<const char*>(&buf[0]), buf.size()); out.close(); }