Implement LZMA file decompression

This commit is contained in:
Vikram Rajkumar 2014-07-02 23:28:08 -04:00
parent f56dd6d208
commit 39c7f05ea9
5 changed files with 44 additions and 37 deletions

5
.gitignore vendored
View file

@ -33,9 +33,11 @@ ZERO_CHECK
Debug/
Release/
CMakeCache.txt
CMakeFiles
Makefile
cmake_install.cmake
*.cmake
libfc.a
libfc_debug.a
@ -45,6 +47,7 @@ fc_automoc.cpp
git_revision.cpp
GitSHA3.cpp
lzma_test
ntp_test
task_cancel_test
udt_client

View file

@ -237,8 +237,8 @@ target_link_libraries( udt_server fc udt )
add_executable( udt_client tests/udtc.cpp )
target_link_libraries( udt_client fc udt )
add_executable( lzma_compress_file tests/lzma_compress_file.cpp )
target_link_libraries( lzma_compress_file fc )
add_executable( lzma_test tests/lzma_test.cpp )
target_link_libraries( lzma_test fc )
#add_executable( test_compress tests/compress.cpp )
#target_link_libraries( test_compress fc )

View file

@ -8,9 +8,12 @@ namespace fc {
std::vector<char> lzma_compress( const std::vector<char>& in );
std::vector<char> lzma_decompress( const std::vector<char>& compressed );
void lzma_compress_file( path src_path,
path dst_path,
void lzma_compress_file( const path& src_path,
const path& dst_path,
unsigned char level = 5,
unsigned int dict_size = (1 << 20) );
void lzma_decompress_file( const path& src_path,
const path& dst_path );
} // namespace fc

View file

@ -1,15 +1,8 @@
#include <boost/iostreams/device/mapped_file.hpp>
#include <fc/compress/lzma.hpp>
#include <fc/exception/exception.hpp>
#include <lzma_c.h>
#include <boost/iostreams/device/mapped_file.hpp>
#include <fstream>
#include <iostream>
#include <lzma_c.h>
namespace fc {
@ -120,8 +113,8 @@ static size_t output_callback( void* output_ctx, const void* output_buf, size_t
return output_len;
}
void lzma_compress_file( path src_path,
path dst_path,
void lzma_compress_file( const path& src_path,
const path& dst_path,
unsigned char level,
unsigned int dict_size )
{
@ -170,29 +163,36 @@ void lzma_compress_file( path src_path,
elzma_compress_free( &handle );
FC_ASSERT( rc == ELZMA_E_OK );
}
void lzma_decompress_file( const path& src_path,
const path& dst_path )
{
FC_ASSERT( exists( src_path ) );
FC_ASSERT( !exists( dst_path ) );
/* TEST */
FC_ASSERT( exists( dst_path ) );
boost::iostreams::mapped_file_source src_file;
src_file.open( src_path.string() );
FC_ASSERT( src_file.is_open() );
boost::iostreams::mapped_file_source dst_file;
dst_file.open( dst_path.string() );
FC_ASSERT( dst_file.is_open() );
elzma_decompress_handle handle = NULL;
handle = elzma_decompress_alloc();
FC_ASSERT( handle != NULL );
std::vector<char> result( dst_file.data(), dst_file.data() + dst_file.size() );
dst_file.close();
struct lzma_file_ctx ctx;
ctx.src_buf = ( const unsigned char* )src_file.data();
ctx.src_len = src_file.size();
ctx.dst_path = dst_path;
for( const auto& c : result )
{
std::cout << c;
}
std::cout << "\n";
auto rc = elzma_decompress_run( handle,
input_callback,
( void * )&ctx,
output_callback,
( void * )&ctx,
elzma_file_format::ELZMA_lzma );
result = lzma_decompress( result );
for( const auto& c : result )
{
std::cout << c;
}
elzma_decompress_free( &handle );
FC_ASSERT( rc == ELZMA_E_OK );
}
} // namespace fc

View file

@ -8,16 +8,17 @@ using namespace fc;
int main( int argc, char** argv )
{
if( argc != 2 && argc != 3 )
if( argc != 2 )
{
std::cout << "usage: " << argv[0] << " <src_path> [dst_path = src_path.lzma]\n";
std::cout << "usage: " << argv[0] << " <filename>\n";
exit( -1 );
}
auto src = std::string( argv[1] );
auto dst = (argc == 3) ? std::string( argv[2] ) : src + ".lzma";
auto dst = src + ".compressed";
lzma_compress_file( src, dst );
lzma_decompress_file( dst, src + ".decompressed" );
return 0;
}