Main Page   Class Hierarchy   Compound List   File List   Compound Members  

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 }
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 //    cout << "writing " << ent_name << " to " << outfile << endl ;
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   Zipios++ - a small C++ library that provides easy access to .zip files.
00109   Copyright (C) 2000  Thomas Søndergaard
00110   
00111   This library is free software; you can redistribute it and/or
00112   modify it under the terms of the GNU Lesser General Public
00113   License as published by the Free Software Foundation; either
00114   version 2 of the License, or (at your option) any later version.
00115   
00116   This library is distributed in the hope that it will be useful,
00117   but WITHOUT ANY WARRANTY; without even the implied warranty of
00118   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00119   Lesser General Public License for more details.
00120   
00121   You should have received a copy of the GNU Lesser General Public
00122   License along with this library; if not, write to the Free Software
00123   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00124 */

Generated at Tue Aug 14 20:39:26 2001 for Zipios++ by doxygen1.2.0 written by Dimitri van Heesch, © 1997-2000