| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Authors: Catalin Popescu |
|---|
| 31 |
|
|---|
| 32 |
// Showcase for compressing / decompressing a file |
|---|
| 33 |
|
|---|
| 34 |
#include <sys/stat.h> |
|---|
| 35 |
#include <fcntl.h> |
|---|
| 36 |
#include <stdlib.h> |
|---|
| 37 |
#include <stdio.h> |
|---|
| 38 |
#include <unistd.h> |
|---|
| 39 |
#include <sys/types.h> |
|---|
| 40 |
|
|---|
| 41 |
#include "common/base/types.h" |
|---|
| 42 |
#include "common/base/log.h" |
|---|
| 43 |
#include "common/base/system.h" |
|---|
| 44 |
#include "common/base/gflags.h" |
|---|
| 45 |
#include "common/base/errno.h" |
|---|
| 46 |
|
|---|
| 47 |
#include "common/io/zlib/zlibwrapper.h" |
|---|
| 48 |
#include "common/io/file/file.h" |
|---|
| 49 |
|
|---|
| 50 |
////////////////////////////////////////////////////////////////////// |
|---|
| 51 |
|
|---|
| 52 |
DEFINE_string(infile, |
|---|
| 53 |
"", |
|---|
| 54 |
"The input file"); |
|---|
| 55 |
DEFINE_string(outfile, |
|---|
| 56 |
"", |
|---|
| 57 |
"The outputput file"); |
|---|
| 58 |
DEFINE_bool(compress, |
|---|
| 59 |
true, |
|---|
| 60 |
"True -> compress / else decompress"); |
|---|
| 61 |
|
|---|
| 62 |
////////////////////////////////////////////////////////////////////// |
|---|
| 63 |
|
|---|
| 64 |
void ProcessCompressFileInput(io::ZlibDeflateWrapper* zwrapper, |
|---|
| 65 |
int32* size, |
|---|
| 66 |
io::File* fout, |
|---|
| 67 |
io::MemoryStream* in) { |
|---|
| 68 |
io::MemoryStream out; |
|---|
| 69 |
LOG_INFO << " Got to process: " << in->Size(); |
|---|
| 70 |
if ( !zwrapper->DeflateSize(in, &out, size) ) { |
|---|
| 71 |
LOG_FATAL << " Error in compression - damn.. "; |
|---|
| 72 |
} |
|---|
| 73 |
if ( !out.IsEmpty() ) { |
|---|
| 74 |
LOG_INFO << "Process - left: " << *size << " got : " << out.Size(); |
|---|
| 75 |
string s; |
|---|
| 76 |
out.ReadString(&s); |
|---|
| 77 |
LOG_INFO << " Writing: " << s.size(); |
|---|
| 78 |
fout->Write(s.data(), s.size()); |
|---|
| 79 |
} |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
void ProcessDecompressFileInput(io::ZlibInflateWrapper* zwrapper, |
|---|
| 83 |
int32* size, |
|---|
| 84 |
io::File* fout, |
|---|
| 85 |
io::MemoryStream* in) { |
|---|
| 86 |
io::MemoryStream out; |
|---|
| 87 |
LOG_INFO << " Got to process: " << in->Size(); |
|---|
| 88 |
int err = zwrapper->InflateSize(in, &out, size); |
|---|
| 89 |
if ( err == Z_STREAM_END ) { |
|---|
| 90 |
LOG_INFO << " Decompress done !!!"; |
|---|
| 91 |
} else if ( err != Z_OK ) { |
|---|
| 92 |
LOG_FATAL << " Error in decompression !" << err; |
|---|
| 93 |
} |
|---|
| 94 |
if ( !out.IsEmpty() ) { |
|---|
| 95 |
LOG_INFO << "Process - left: " << *size << " got : " << out.Size(); |
|---|
| 96 |
string s; |
|---|
| 97 |
out.ReadString(&s); |
|---|
| 98 |
LOG_INFO << " Writing: " << s.size(); |
|---|
| 99 |
fout->Write(s.data(), s.size()); |
|---|
| 100 |
} |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
int main(int argc, char* argv[]) { |
|---|
| 104 |
common::Init(argc, argv); |
|---|
| 105 |
CHECK(!FLAGS_infile.empty()) << " Please specify an input file !"; |
|---|
| 106 |
CHECK(!FLAGS_outfile.empty()) << " Please specify an output file !"; |
|---|
| 107 |
|
|---|
| 108 |
io::File* fin = io::File::OpenFileOrDie(FLAGS_infile.c_str()); |
|---|
| 109 |
int32 file_size = fin->Size(); |
|---|
| 110 |
io::File* fout = io::File::CreateFileOrDie(FLAGS_outfile.c_str()); |
|---|
| 111 |
io::ZlibInflateWrapper decompress_wrapper; |
|---|
| 112 |
io::ZlibDeflateWrapper compress_wrapper; |
|---|
| 113 |
char buffer[65536]; |
|---|
| 114 |
int cb = 0; |
|---|
| 115 |
io::MemoryStream in; |
|---|
| 116 |
while ( (cb = fin->Read(buffer, sizeof(buffer))) > 0 ) { |
|---|
| 117 |
in.Write(buffer, cb); |
|---|
| 118 |
if ( FLAGS_compress ) { |
|---|
| 119 |
ProcessCompressFileInput(&compress_wrapper, &file_size, fout, &in); |
|---|
| 120 |
} else { |
|---|
| 121 |
ProcessDecompressFileInput(&decompress_wrapper, &file_size, fout, &in); |
|---|
| 122 |
} |
|---|
| 123 |
} |
|---|
| 124 |
delete fin; |
|---|
| 125 |
delete fout; |
|---|
| 126 |
} |
|---|