root/trunk/whisperlib/common/io/zlib/zlib-gzip.cc

Revision 7, 3.1 kB (checked in by whispercastorg, 2 years ago)

version 0.2.0

Line 
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_input_stream.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 int main(int argc, char* argv[]) {
65   common::Init(argc, argv);
66   CHECK(!FLAGS_infile.empty())  << " Please specify an input file !";
67   CHECK(!FLAGS_outfile.empty()) << " Please specify an output file !";
68
69   io::MemoryStream ms;
70   ms.Write(io::FileInputStream::ReadFileOrDie(FLAGS_infile.c_str()));
71   io::MemoryStream out;
72   if ( FLAGS_compress ) {
73     io::ZlibGzipEncodeWrapper zw;
74     zw.Encode(&ms, &out);
75   } else {
76     io::ZlibGzipDecodeWrapper zw;
77     int err = zw.Decode(&ms, &out);
78     CHECK_EQ(err, Z_STREAM_END) << " ZLIB error: " << err;
79   }
80   if ( !out.IsEmpty() ) {
81     io::File* fout = io::File::CreateFileOrDie(FLAGS_outfile.c_str());
82     const string s(out.ToString());
83     fout->Write(s.data(), s.size());
84     delete fout;
85   }
86 }
Note: See TracBrowser for help on using the browser.