Zipios++
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 <vector>
00007 #include <sstream>
00008 #include <stdlib.h>
00009 #include <fstream>
00010 
00011 #include "zipios++/zipfile.h"
00012 #include "zipios++/zipinputstream.h"
00013 #include "src/outputstringstream.h"
00014 
00015 #include "zipfiletest.h"
00016 
00017 using namespace zipios ;
00018 
00019 using std::cerr;
00020 using std::cout;
00021 using std::endl;
00022 using std::auto_ptr;
00023 using std::ifstream;
00024 using std::string;
00025 using std::vector;
00026 
00027 void zipios::ZipFileTest::testUnzip() {
00028   vector<string> entries;
00029   entries.push_back("file1.txt");
00030   entries.push_back("file2.txt");
00031   entries.push_back("file3.txt");
00032   entries.push_back("testfile.bin");
00033   
00034   ZipFile zipFile("test.zip");
00035   CPPUNIT_ASSERT_EQUAL(4, zipFile.size());
00036   compareZipFile("test.zip", entries);
00037 }
00038 
00039 void zipios::ZipFileTest::testZipUnzip() {      
00040   const string zipFileName = "gentest.zip";
00041   vector<string> entries;
00042   entries.push_back("testfile.bin");
00043   entries.push_back("Makefile.in");
00044   entries.push_back("zipfiletest.cpp");
00045   entries.push_back("zipfiletest.h");
00046   entries.push_back("all_tests");
00047   writeZipFile(zipFileName, entries);
00048   compareZipFile(zipFileName, entries);
00049 }
00050 
00051 void zipios::ZipFileTest::testComment(){
00052   // ZipFile zipFile("test.zip");
00053   //CPPUNIT_ASSERT_EQUAL("something", zipFile.getComment());
00054 } 
00055 
00056 void zipios::ZipFileTest::writeZipFile(const string &zipFileName, vector<string> entryFileNames) {
00057   ZipOutputStream zos(zipFileName);
00058   std::vector<string>::const_iterator it = entryFileNames.begin();
00059   for ( ; it != entryFileNames.end() ; ++it ) {
00060     writeFileToZipOutputStream(zos, *it);
00061   }
00062   zos.close();
00063 }
00064 
00065 void zipios::ZipFileTest::compareZipFile(const string &zipFileName, vector<string> entryFileNames) {
00066   using namespace std;
00067   ZipFile zipFile(zipFileName);
00068   vector<string>::const_iterator it = entryFileNames.begin();
00069   for ( ; it != entryFileNames.end() ; ++it ) {
00070     auto_ptr<istream> zis(zipFile.getInputStream(*it));
00071         if (zis.get() == 0)
00072                 CPPUNIT_FAIL("Entry '"+*it+"' not found in zip file");
00073     ifstream fis((*it).c_str(), ios::in | ios::binary);
00074     compareStreams(*it, *zis, fis);
00075   }
00076 }
00077 
00078 void zipios::ZipFileTest::writeFileToZipOutputStream( ZipOutputStream &zos, const string &filename ) {
00079   zos.putNextEntry( ZipCDirEntry( filename ) ) ;
00080   ifstream ifs( filename.c_str(), ios::in | ios::binary ) ;
00081   if (! ifs)
00082           CPPUNIT_FAIL("Could not open file '"+filename+"'");
00083   zos << ifs.rdbuf() ; 
00084 
00085 //    cerr << "ostream Stream state: "  ;
00086 //    cerr << "good() = " << zos.good() << ",\t" ;
00087 //    cerr << "fail() = " << zos.fail() << ",\t" ;
00088 //    cerr << "bad()  = " << zos.bad()  << ",\t" ;
00089 //    cerr << "eof()  = " << zos.eof()  << endl  ;
00090 
00091 //    cerr << "istream Stream state: "  ;
00092 //    cerr << "good() = " << ifs.good() << ",\t" ;
00093 //    cerr << "fail() = " << ifs.fail() << ",\t" ;
00094 //    cerr << "bad()  = " << ifs.bad()  << ",\t" ;
00095 //    cerr << "eof()  = " << ifs.eof()  << endl  ;
00096 
00097 }
00098 
00099 void zipios::ZipFileTest::compareStreams(const std::string& entryName,
00100                                          istream &is1, istream &is2) {
00101   OutputStringStream buf1, buf2;
00102   buf1 << is1.rdbuf();
00103   buf2 << is2.rdbuf();
00104   CPPUNIT_ASSERT_MESSAGE("Streams differ for entry '"+entryName+"'", 
00105                          buf1.str() == buf2.str());
00106 }
00107 
00108 void zipios::ZipFileTest::testClone(){
00109  ZipFile zipFile("test.zip");
00110  std::cout<<"Testing cloning..need some help here"<<std::endl;
00111  // FileCollection newzip = clone("test.zip");
00112  //newzip.clone("test.zip");
00113  
00114  
00115 }
00116 
00122 /*
00123   Zipios++ - a small C++ library that provides easy access to .zip files.
00124   Copyright (C) 2000  Thomas Søndergaard
00125   
00126   This library is free software; you can redistribute it and/or
00127   modify it under the terms of the GNU Lesser General Public
00128   License as published by the Free Software Foundation; either
00129   version 2 of the License, or (at your option) any later version.
00130   
00131   This library is distributed in the hope that it will be useful,
00132   but WITHOUT ANY WARRANTY; without even the implied warranty of
00133   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00134   Lesser General Public License for more details.
00135   
00136   You should have received a copy of the GNU Lesser General Public
00137   License along with this library; if not, write to the Free Software
00138   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00139 */
00140