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/ Debug/
Release/ Release/
CMakeCache.txt
CMakeFiles CMakeFiles
Makefile Makefile
cmake_install.cmake *.cmake
libfc.a libfc.a
libfc_debug.a libfc_debug.a
@ -45,6 +47,7 @@ fc_automoc.cpp
git_revision.cpp git_revision.cpp
GitSHA3.cpp GitSHA3.cpp
lzma_test
ntp_test ntp_test
task_cancel_test task_cancel_test
udt_client udt_client

View file

@ -237,8 +237,8 @@ target_link_libraries( udt_server fc udt )
add_executable( udt_client tests/udtc.cpp ) add_executable( udt_client tests/udtc.cpp )
target_link_libraries( udt_client fc udt ) target_link_libraries( udt_client fc udt )
add_executable( lzma_compress_file tests/lzma_compress_file.cpp ) add_executable( lzma_test tests/lzma_test.cpp )
target_link_libraries( lzma_compress_file fc ) target_link_libraries( lzma_test fc )
#add_executable( test_compress tests/compress.cpp ) #add_executable( test_compress tests/compress.cpp )
#target_link_libraries( test_compress fc ) #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_compress( const std::vector<char>& in );
std::vector<char> lzma_decompress( const std::vector<char>& compressed ); std::vector<char> lzma_decompress( const std::vector<char>& compressed );
void lzma_compress_file( path src_path, void lzma_compress_file( const path& src_path,
path dst_path, const path& dst_path,
unsigned char level = 5, unsigned char level = 5,
unsigned int dict_size = (1 << 20) ); unsigned int dict_size = (1 << 20) );
void lzma_decompress_file( const path& src_path,
const path& dst_path );
} // namespace fc } // namespace fc

View file

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

View file

@ -8,16 +8,17 @@ using namespace fc;
int main( int argc, char** argv ) 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 ); exit( -1 );
} }
auto src = std::string( argv[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_compress_file( src, dst );
lzma_decompress_file( dst, src + ".decompressed" );
return 0; return 0;
} }