00001
00002 #include "zipios++/zipios-config.h"
00003
00004 #include "zipios++/meta-iostreams.h"
00005 #include <memory>
00006 #include <stdlib.h>
00007
00008 #include "zipios++/zipfile.h"
00009 #include "zipios++/zipinputstream.h"
00010
00011 using namespace zipios ;
00012
00013 using std::cerr ;
00014 using std::cout ;
00015 using std::endl ;
00016 using std::auto_ptr ;
00017 using std::ofstream ;
00018
00019 void entryToFile( const string &ent_name, istream &is, const string &outfile,
00020 bool cerr_report = false ) ;
00021
00022 int main() {
00023 try {
00024 const string name_zipfile( "test.zip" ) ;
00025 const string name_entry1 ( "file1.txt" ) ;
00026 const string name_entry2 ( "file2.txt" ) ;
00027 const string name_entry3 ( "file3.txt" ) ;
00028 const string unzip_ext ( "unzipped" ) ;
00029 const string name_uz1 ( name_entry1 + unzip_ext ) ;
00030 const string name_uz2 ( name_entry2 + unzip_ext ) ;
00031 const string name_uz3 ( name_entry3 + unzip_ext ) ;
00032 const string diffcmd ( "diff -q " ) ;
00033 const string unzipcmd ( "unzip -oq " ) ;
00034
00035 ZipFile rzf( name_zipfile ) ;
00036 ZipFile zf( rzf ) ;
00037
00038
00039 ConstEntries entries = zf.entries() ;
00040
00041
00042
00043
00044
00045
00046
00047
00048 ConstEntryPointer ent = zf.getEntry( name_entry2, FileCollection::IGNORE ) ;
00049 if ( ent != 0 ) {
00050 auto_ptr< istream > is( zf.getInputStream( ent ) ) ;
00051 entryToFile( name_entry2, *is, name_uz2 ) ;
00052 }
00053
00054
00055 ent = zf.getEntry( name_entry1, FileCollection::IGNORE ) ;
00056 if ( ent != 0 ) {
00057 auto_ptr< istream > is( zf.getInputStream( ent ) ) ;
00058 entryToFile( name_entry1, *is, name_uz1 ) ;
00059 }
00060
00061
00062 ZipInputStream zf2( name_zipfile ) ;
00063 zf2.getNextEntry() ;
00064 zf2.getNextEntry() ;
00065 entryToFile( name_entry3, zf2, name_uz3 ) ;
00066
00067
00068 system( string( unzipcmd + name_zipfile + " " + name_entry1 + " " +
00069 name_entry2 + " " + name_entry3 ).c_str() ) ;
00070
00071
00072
00073 return system( string( diffcmd + name_uz1 + " " + name_entry1 ).c_str() ) +
00074 system( string( diffcmd + name_uz2 + " " + name_entry2 ).c_str() ) +
00075 system( string( diffcmd + name_uz3 + " " + name_entry3 ).c_str() ) ;
00076 }
00077 catch( exception &excp ) {
00078 cerr << "Exception caught in main() :" << endl ;
00079 cerr << excp.what() << endl ;
00080 }
00081 }
00082
00083
00084 void entryToFile( const string &ent_name, istream &is, const string &outfile,
00085 bool cerr_report ) {
00086 ofstream ofs( outfile.c_str(), ios::out | ios::binary ) ;
00087
00088
00089
00090 ofs << is.rdbuf() ;
00091 if ( cerr_report ) {
00092 cerr << "Stream state: " ;
00093 cerr << "good() = " << is.good() << ",\t" ;
00094 cerr << "fail() = " << is.fail() << ",\t" ;
00095 cerr << "bad() = " << is.bad() << ",\t" ;
00096 cerr << "eof() = " << is.eof() << endl << endl;
00097 }
00098 ofs.close() ;
00099 }
00100
00101
00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 00123 00124