Zipios++
test_zip.cpp
Go to the documentation of this file.
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 ) ; // Test copy constructor
00037 //      ZipFile zf( name_zipfile ) ;
00038 
00039     ConstEntries entries = zf.entries() ;
00040 //      cout << "\nEntries (" << zf.size() <<  "):\n" ;
00041 //      ConstEntries::iterator it ;
00042 //      for( it = entries.begin() ; it != entries.end() ; it++)
00043 //        cout << *(*it) << endl ;
00044 //      cout << "\n" ;
00045     
00046     
00047     // Unzip second entry
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     // Unzip first entry
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     // Unzip third entry, by use of ZipInputStream alone
00062     ZipInputStream zf2( name_zipfile ) ;
00063     zf2.getNextEntry() ;
00064     zf2.getNextEntry() ;
00065     entryToFile( name_entry3, zf2, name_uz3 ) ;
00066     
00067 //      cerr << "Unzipping entries using 'unzip' to get references to 'diff' against :\n" ;
00068     system( string( unzipcmd + name_zipfile + " " + name_entry1 + " " + 
00069                     name_entry2 + " " + name_entry3 ).c_str() ) ;
00070 //      cerr << "\nOutput from " << diffcmd << " :\n" ;
00071 
00072     // Fail if any one of the fails
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   return -1;
00082 }
00083 
00084 
00085 void entryToFile( const string &, istream &is, const string &outfile,
00086                   bool cerr_report ) {
00087   ofstream ofs( outfile.c_str(), ios::out | ios::binary ) ;
00088 
00089 //    cout << "writing " << ent_name << " to " << outfile << endl ;
00090 
00091   ofs << is.rdbuf() ;
00092   if ( cerr_report ) {
00093     cerr << "Stream state: "  ;
00094     cerr << "good() = " << is.good() << ",\t" ;
00095     cerr << "fail() = " << is.fail() << ",\t" ;
00096     cerr << "bad()  = " << is.bad()  << ",\t" ;
00097     cerr << "eof()  = " << is.eof()  << endl << endl;
00098   }
00099   ofs.close() ;
00100 }
00101    
00102 
00108 /*
00109   Zipios++ - a small C++ library that provides easy access to .zip files.
00110   Copyright (C) 2000  Thomas Søndergaard
00111   
00112   This library is free software; you can redistribute it and/or
00113   modify it under the terms of the GNU Lesser General Public
00114   License as published by the Free Software Foundation; either
00115   version 2 of the License, or (at your option) any later version.
00116   
00117   This library is distributed in the hope that it will be useful,
00118   but WITHOUT ANY WARRANTY; without even the implied warranty of
00119   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00120   Lesser General Public License for more details.
00121   
00122   You should have received a copy of the GNU Lesser General Public
00123   License along with this library; if not, write to the Free Software
00124   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00125 */