1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
/*
Copyright (c) 2006 - 2018
CLST - Radboud University
ILK - Tilburg University
This file is part of ticcutils
ticcutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ticcutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
For questions and suggestions, see:
https://github.com/LanguageMachines/ticcutils/issues
or send mail to:
lamasoftware (at ) science.ru.nl
*/
#include "ticcutils/zipper.h"
#include <stdexcept>
#include <fstream>
#include "config.h"
#include "bzlib.h"
#include "ticcutils/bz2stream.h"
#include "ticcutils/gzstream.h"
using namespace std;
namespace TiCC {
bool bz2Compress( const string& inName, const string& outName ){
std::ifstream infile( inName, std::ios::binary);
if ( !infile ){
cerr << "bz2: unable to open inputfile: " << inName << endl;
return false;
}
string outname = outName;
if ( outname.empty() )
outname = inName + ".bz2";
std::ofstream outfile( outname, std::ios::binary);
if ( !outfile ){
cerr << "bz2: unable to open outputfile: " << outname << endl;
return false;
}
bz2ostream bzout(outfile.rdbuf());
bzout << infile.rdbuf();
return true;
}
bool bz2Decompress( const string& inName, const string& outName ){
std::ifstream infile( inName, std::ios::binary);
if ( !infile ){
cerr << "bz2: unable to open inputfile: " << inName << endl;
return false;
}
string outname = outName;
if ( outname.empty() ){
outname = inName;
string::size_type pos = outname.rfind( ".bz2" );
if ( pos == string::npos ){
cerr << "bz2: expected an inputfile name with .bz2: " << inName << endl;
return false;
}
else {
outname = outname.substr( 0, pos );
}
}
std::ofstream outfile( outname, std::ios::binary);
if ( !infile ){
cerr << "bz2: unable to open inputfile: " << inName << endl;
return false;
}
bz2istream bz2in(infile.rdbuf());
outfile << bz2in.rdbuf();
return true;
}
string bz2ReadStream( istream& is ){
bz2istream bz2in(is.rdbuf());
istreambuf_iterator<char> inpos( bz2in.rdbuf() );
istreambuf_iterator<char> endpos;
string result;
while ( inpos != endpos ){
result += *inpos;
++inpos;
}
return result;
}
string bz2ReadFile( const string& inName ){
string inname = inName;
string::size_type pos = inname.rfind( ".bz2" );
if ( pos == string::npos ){
throw runtime_error( "bz2: expected an inputfile name with .bz2 extension" );
}
std::ifstream infile( inname, std::ios::binary);
if ( !infile ){
throw runtime_error( "bz2: unable to open inputfile: " + inName );
}
return bz2ReadStream( infile );
}
bool bz2WriteStream( ostream& outfile, const string& buffer ){
bz2ostream bzout(outfile.rdbuf());
bzout << buffer;
return true;
}
bool bz2WriteFile( const string& outName, const string& buffer ){
std::ofstream outfile( outName, std::ios::binary);
if ( !outfile ){
cerr << "bz2: unable to open outputfile: " << outName << endl;
return false;
}
bz2ostream bzout(outfile.rdbuf());
bzout << buffer;
return true;
}
string gzReadStream( istream& is ){
string result;
char c;
while ( is.get(c) )
result += c;
return result;
}
string gzReadFile( const string& inName ){
string inname = inName;
string::size_type pos = inname.rfind( ".gz" );
if ( pos == string::npos ){
throw runtime_error( "gz: expected an inputfile name with .gz extension" );
}
igzstream infile( inname, ios::binary|ios::in );
if ( !infile ){
throw runtime_error( "gz: unable to open inputfile: " + inName );
}
return gzReadStream( infile );
}
bool gzWriteStream( ostream& outfile, const string& buffer ){
outfile << buffer;
return true;
}
bool gzWriteFile( const string& outName, const string& buffer ){
ogzstream outfile( outName, ios::binary|ios::out );
if ( !outfile ){
cerr << "gz: unable to open outputfile: " << outName << endl;
return false;
}
outfile << buffer;
return true;
}
bool gzCompress( const string& inName, const string& outName ){
ifstream infile( inName );
if ( !infile ){
cerr << "gz: unable to open inputfile: " << inName << endl;
return false;
}
string outname = outName;
if ( outname.empty() )
outname = inName + ".gz";
ogzstream outfile( outname, ios::binary|ios::out );
if ( !outfile ){
cerr << "gz: unable to open outputfile: " << outname << endl;
return false;
}
char c;
while ( infile.get(c) ){
outfile << c;
}
infile.close();
outfile.flush();
outfile.close();
return true;
}
bool gzDecompress( const string& inName, const string& outName ){
igzstream infile( inName, std::ios::binary|ios::in);
if ( !infile ){
cerr << "gz: unable to open inputfile: " << inName << endl;
return false;
}
string outname = outName;
if ( outname.empty() ){
outname = inName;
string::size_type pos = outname.rfind( ".gz" );
if ( pos == string::npos ){
cerr << "gz: expected an inputfile name with .gz: " << inName << endl;
return false;
}
else {
outname = outname.substr( 0, pos );
}
}
std::ofstream outfile( outname, std::ios::binary);
if ( !outfile ){
cerr << "gz: unable to open outputfile: " << outName << endl;
return false;
}
char c;
while ( infile.get(c))
outfile << c;
return true;
}
}
|