Main Page   Class Hierarchy   Compound List   File List   Compound Members  

zipfiletest.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 #include "src/outputstringstream.h"
00011 
00012 #include "zipfiletest.h"
00013 
00014 using namespace zipios ;
00015 
00016 using std::cerr ;
00017 using std::cout ;
00018 using std::endl ;
00019 using std::auto_ptr ;
00020 using std::ofstream ;
00021 using std::ifstream ;
00022 using std::string;
00023 
00024 
00025 void zipios::ZipFileTest::runTests() {
00026   const string zipFileName = "gentest.zip";
00027   vector<string> entries;
00028   entries.push_back("all_tests");
00029   entries.push_back("Makefile.in");
00030   entries.push_back("zipfiletest.cpp");
00031   entries.push_back("zipfiletest.h");
00032   entries.push_back("testcase.h");
00033   writeZipFile(zipFileName, entries);
00034   compareZipFile(zipFileName, entries);
00035 }
00036 
00037 
00038 void zipios::ZipFileTest::writeZipFile(const string &zipFileName, vector<string> entryFileNames) {
00039   ZipOutputStream zos(zipFileName);
00040   vector<string>::const_iterator it = entryFileNames.begin();
00041   for ( ; it != entryFileNames.end() ; ++it ) {
00042     writeFileToZipOutputStream(zos, *it);
00043   }
00044   zos.close();
00045 }
00046 
00047 void zipios::ZipFileTest::compareZipFile(const string &zipFileName, vector<string> entryFileNames) {
00048   using namespace std;
00049   ZipFile zipFile(zipFileName);
00050   vector<string>::const_iterator it = entryFileNames.begin();
00051   for ( ; it != entryFileNames.end() ; ++it ) {
00052     auto_ptr<istream> zis(zipFile.getInputStream(*it));
00053     ifstream fis((*it).c_str());
00054     try {
00055       compareStreams(*zis, fis);
00056     } catch(Exception &ex) {
00057       throw Exception("For zip entry '"+*it+"': "+ex.what());
00058     }
00059   }
00060 }
00061 
00062 void zipios::ZipFileTest::writeFileToZipOutputStream( ZipOutputStream &zos, const string &filename ) {
00063   zos.putNextEntry( ZipCDirEntry( filename ) ) ;
00064 
00065   ifstream ifs( filename.c_str(), ios::in | ios::binary ) ;
00066 
00067   zos << ifs.rdbuf() ; 
00068 
00069 //    cerr << "ostream Stream state: "  ;
00070 //    cerr << "good() = " << zos.good() << ",\t" ;
00071 //    cerr << "fail() = " << zos.fail() << ",\t" ;
00072 //    cerr << "bad()  = " << zos.bad()  << ",\t" ;
00073 //    cerr << "eof()  = " << zos.eof()  << endl  ;
00074 
00075 //    cerr << "istream Stream state: "  ;
00076 //    cerr << "good() = " << ifs.good() << ",\t" ;
00077 //    cerr << "fail() = " << ifs.fail() << ",\t" ;
00078 //    cerr << "bad()  = " << ifs.bad()  << ",\t" ;
00079 //    cerr << "eof()  = " << ifs.eof()  << endl  ;
00080 
00081 }
00082 
00083 void zipios::ZipFileTest::compareStreams(istream &is1, istream &is2) {
00084   OutputStringStream buf1, buf2;
00085   buf1 << is1.rdbuf();
00086   buf2 << is2.rdbuf();
00087   if ( buf1.str() != buf2.str() )
00088     throw Exception("contents differ"); // defined at the bottom of fcolexceptions.h
00089 }
00090 
00096 /*
00097   Zipios++ - a small C++ library that provides easy access to .zip files.
00098   Copyright (C) 2000  Thomas Søndergaard
00099   
00100   This library is free software; you can redistribute it and/or
00101   modify it under the terms of the GNU Lesser General Public
00102   License as published by the Free Software Foundation; either
00103   version 2 of the License, or (at your option) any later version.
00104   
00105   This library is distributed in the hope that it will be useful,
00106   but WITHOUT ANY WARRANTY; without even the implied warranty of
00107   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00108   Lesser General Public License for more details.
00109   
00110   You should have received a copy of the GNU Lesser General Public
00111   License along with this library; if not, write to the Free Software
00112   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00113 */
00114 

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