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
00053
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
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
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
00112
00113
00114
00115 }
00116
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140