Zipios++
test_zip.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 <stdlib.h>
7 
8 #include "zipios++/zipfile.h"
10 
11 using namespace zipios ;
12 
13 using std::cerr ;
14 using std::cout ;
15 using std::endl ;
16 using std::auto_ptr ;
17 using std::ofstream ;
18 
19 void entryToFile( const string &ent_name, istream &is, const string &outfile,
20  bool cerr_report = false ) ;
21 
22 int main() {
23  try {
24  const string name_zipfile( "test.zip" ) ;
25  const string name_entry1 ( "file1.txt" ) ;
26  const string name_entry2 ( "file2.txt" ) ;
27  const string name_entry3 ( "file3.txt" ) ;
28  const string unzip_ext ( "unzipped" ) ;
29  const string name_uz1 ( name_entry1 + unzip_ext ) ;
30  const string name_uz2 ( name_entry2 + unzip_ext ) ;
31  const string name_uz3 ( name_entry3 + unzip_ext ) ;
32  const string diffcmd ( "diff -q " ) ;
33  const string unzipcmd ( "unzip -oq " ) ;
34 
35  ZipFile rzf( name_zipfile ) ;
36  ZipFile zf( rzf ) ; // Test copy constructor
37 // ZipFile zf( name_zipfile ) ;
38 
39  ConstEntries entries = zf.entries() ;
40 // cout << "\nEntries (" << zf.size() << "):\n" ;
41 // ConstEntries::iterator it ;
42 // for( it = entries.begin() ; it != entries.end() ; it++)
43 // cout << *(*it) << endl ;
44 // cout << "\n" ;
45 
46 
47  // Unzip second entry
48  ConstEntryPointer ent = zf.getEntry( name_entry2, FileCollection::IGNORE ) ;
49  if ( ent != 0 ) {
50  auto_ptr< istream > is( zf.getInputStream( ent ) ) ;
51  entryToFile( name_entry2, *is, name_uz2 ) ;
52  }
53 
54  // Unzip first entry
55  ent = zf.getEntry( name_entry1, FileCollection::IGNORE ) ;
56  if ( ent != 0 ) {
57  auto_ptr< istream > is( zf.getInputStream( ent ) ) ;
58  entryToFile( name_entry1, *is, name_uz1 ) ;
59  }
60 
61  // Unzip third entry, by use of ZipInputStream alone
62  ZipInputStream zf2( name_zipfile ) ;
63  zf2.getNextEntry() ;
64  zf2.getNextEntry() ;
65  entryToFile( name_entry3, zf2, name_uz3 ) ;
66 
67 // cerr << "Unzipping entries using 'unzip' to get references to 'diff' against :\n" ;
68  system( string( unzipcmd + name_zipfile + " " + name_entry1 + " " +
69  name_entry2 + " " + name_entry3 ).c_str() ) ;
70 // cerr << "\nOutput from " << diffcmd << " :\n" ;
71 
72  // Fail if any one of the fails
73  return system( string( diffcmd + name_uz1 + " " + name_entry1 ).c_str() ) +
74  system( string( diffcmd + name_uz2 + " " + name_entry2 ).c_str() ) +
75  system( string( diffcmd + name_uz3 + " " + name_entry3 ).c_str() ) ;
76  }
77  catch( exception &excp ) {
78  cerr << "Exception caught in main() :" << endl ;
79  cerr << excp.what() << endl ;
80  }
81  return -1;
82 }
83 
84 
85 void entryToFile( const string &, istream &is, const string &outfile,
86  bool cerr_report ) {
87  ofstream ofs( outfile.c_str(), ios::out | ios::binary ) ;
88 
89 // cout << "writing " << ent_name << " to " << outfile << endl ;
90 
91  ofs << is.rdbuf() ;
92  if ( cerr_report ) {
93  cerr << "Stream state: " ;
94  cerr << "good() = " << is.good() << ",\t" ;
95  cerr << "fail() = " << is.fail() << ",\t" ;
96  cerr << "bad() = " << is.bad() << ",\t" ;
97  cerr << "eof() = " << is.eof() << endl << endl;
98  }
99  ofs.close() ;
100 }
101 
102 
108 /*
109  Zipios++ - a small C++ library that provides easy access to .zip files.
110  Copyright (C) 2000 Thomas Søndergaard
111 
112  This library is free software; you can redistribute it and/or
113  modify it under the terms of the GNU Lesser General Public
114  License as published by the Free Software Foundation; either
115  version 2 of the License, or (at your option) any later version.
116 
117  This library is distributed in the hope that it will be useful,
118  but WITHOUT ANY WARRANTY; without even the implied warranty of
119  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
120  Lesser General Public License for more details.
121 
122  You should have received a copy of the GNU Lesser General Public
123  License along with this library; if not, write to the Free Software
124  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
125 */
Header file that defines ZipFile.
SimpleSmartPointer is a simple reference counting smart pointer template.
Header file that defines ZipInputStream.