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
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
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");
00089 }
00090
00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113
00114