Zipios++
zipfiletest.cpp
Go to the documentation of this file.
1 
2 #include "zipios++/zipios-config.h"
3 
4 #include "zipios++/meta-iostreams.h"
5 #include <memory>
6 #include <vector>
7 #include <sstream>
8 #include <stdlib.h>
9 #include <fstream>
10 
11 #include "zipios++/zipfile.h"
13 #include "src/outputstringstream.h"
14 
15 #include "zipfiletest.h"
16 
17 using namespace zipios ;
18 
19 using std::cerr;
20 using std::cout;
21 using std::endl;
22 using std::auto_ptr;
23 using std::ifstream;
24 using std::string;
25 using std::vector;
26 
27 void zipios::ZipFileTest::testUnzip() {
28  vector<string> entries;
29  entries.push_back("file1.txt");
30  entries.push_back("file2.txt");
31  entries.push_back("file3.txt");
32  entries.push_back("testfile.bin");
33 
34  ZipFile zipFile("test.zip");
35  CPPUNIT_ASSERT_EQUAL(4, zipFile.size());
36  compareZipFile("test.zip", entries);
37 }
38 
39 void zipios::ZipFileTest::testZipUnzip() {
40  const string zipFileName = "gentest.zip";
41  vector<string> entries;
42  entries.push_back("testfile.bin");
43  entries.push_back("Makefile.in");
44  entries.push_back("zipfiletest.cpp");
45  entries.push_back("zipfiletest.h");
46  entries.push_back("all_tests");
47  writeZipFile(zipFileName, entries);
48  compareZipFile(zipFileName, entries);
49 }
50 
51 void zipios::ZipFileTest::testComment(){
52  // ZipFile zipFile("test.zip");
53  //CPPUNIT_ASSERT_EQUAL("something", zipFile.getComment());
54 }
55 
56 void zipios::ZipFileTest::writeZipFile(const string &zipFileName, vector<string> entryFileNames) {
57  ZipOutputStream zos(zipFileName);
58  std::vector<string>::const_iterator it = entryFileNames.begin();
59  for ( ; it != entryFileNames.end() ; ++it ) {
60  writeFileToZipOutputStream(zos, *it);
61  }
62  zos.close();
63 }
64 
65 void zipios::ZipFileTest::compareZipFile(const string &zipFileName, vector<string> entryFileNames) {
66  using namespace std;
67  ZipFile zipFile(zipFileName);
68  vector<string>::const_iterator it = entryFileNames.begin();
69  for ( ; it != entryFileNames.end() ; ++it ) {
70  auto_ptr<istream> zis(zipFile.getInputStream(*it));
71  if (zis.get() == 0)
72  CPPUNIT_FAIL("Entry '"+*it+"' not found in zip file");
73  ifstream fis((*it).c_str(), ios::in | ios::binary);
74  compareStreams(*it, *zis, fis);
75  }
76 }
77 
78 void zipios::ZipFileTest::writeFileToZipOutputStream( ZipOutputStream &zos, const string &filename ) {
79  zos.putNextEntry( ZipCDirEntry( filename ) ) ;
80  ifstream ifs( filename.c_str(), ios::in | ios::binary ) ;
81  if (! ifs)
82  CPPUNIT_FAIL("Could not open file '"+filename+"'");
83  zos << ifs.rdbuf() ;
84 
85 // cerr << "ostream Stream state: " ;
86 // cerr << "good() = " << zos.good() << ",\t" ;
87 // cerr << "fail() = " << zos.fail() << ",\t" ;
88 // cerr << "bad() = " << zos.bad() << ",\t" ;
89 // cerr << "eof() = " << zos.eof() << endl ;
90 
91 // cerr << "istream Stream state: " ;
92 // cerr << "good() = " << ifs.good() << ",\t" ;
93 // cerr << "fail() = " << ifs.fail() << ",\t" ;
94 // cerr << "bad() = " << ifs.bad() << ",\t" ;
95 // cerr << "eof() = " << ifs.eof() << endl ;
96 
97 }
98 
99 void zipios::ZipFileTest::compareStreams(const std::string& entryName,
100  istream &is1, istream &is2) {
101  OutputStringStream buf1, buf2;
102  buf1 << is1.rdbuf();
103  buf2 << is2.rdbuf();
104  CPPUNIT_ASSERT_MESSAGE("Streams differ for entry '"+entryName+"'",
105  buf1.str() == buf2.str());
106 }
107 
108 void zipios::ZipFileTest::testClone(){
109  ZipFile zipFile("test.zip");
110  std::cout<<"Testing cloning..need some help here"<<std::endl;
111  // FileCollection newzip = clone("test.zip");
112  //newzip.clone("test.zip");
113 
114 
115 }
116 
122 /*
123  Zipios++ - a small C++ library that provides easy access to .zip files.
124  Copyright (C) 2000 Thomas Søndergaard
125 
126  This library is free software; you can redistribute it and/or
127  modify it under the terms of the GNU Lesser General Public
128  License as published by the Free Software Foundation; either
129  version 2 of the License, or (at your option) any later version.
130 
131  This library is distributed in the hope that it will be useful,
132  but WITHOUT ANY WARRANTY; without even the implied warranty of
133  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
134  Lesser General Public License for more details.
135 
136  You should have received a copy of the GNU Lesser General Public
137  License along with this library; if not, write to the Free Software
138  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
139 */
140 
Header file that defines ZipFile.
Specialization of ZipLocalEntry, that add fields for storing the extra information, that is only present in the entries in the zip central directory and not in the local entry headers.
Definition: ziphead.h:102
OutputStringStream is typedefed to ostringstream if sstream is part of the standard library (unless Z...
Header file that defines OutputStringStream.
void putNextEntry(const ZipCDirEntry &entry)
string str()
Specialization of ostrstream::str() that takes care of null-terminating the string and unfreezing the...
Header file that defines ZipInputStream.