zipios  2.2.6
Zipios -- a small C++ library that provides easy access to .zip files.
zipfile.cpp
Go to the documentation of this file.
1 /*
2  Zipios -- a small C++ library that provides easy access to .zip files.
3 
4  Copyright (C) 2000-2007 Thomas Sondergaard
5  Copyright (C) 2015-2019 Made to Order Software Corporation
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 
29 #include "zipios/zipfile.hpp"
30 
32 
33 #include "backbuffer.hpp"
36 #include "zipinputstream.hpp"
37 #include "zipoutputstream.hpp"
38 
39 #include <fstream>
40 
41 
53 namespace zipios
54 {
55 
56 
298 {
299  // open zipfile, read 4 last bytes close file
300  // create ZipFile object.
301  uint32_t start_offset;
302  {
303  std::ifstream ifs(name, std::ios::in | std::ios::binary);
304  ifs.seekg(-4, std::ios::end);
305  zipRead(ifs, start_offset);
306  // todo: add support for 64 bit (files of more than 4Gb)
307  }
308  return ZipFile::pointer_t(new ZipFile(name, start_offset, 4));
309 }
310 
311 
321  //: m_vs(...) -- auto-init
322 {
323 }
324 
325 
344 ZipFile::ZipFile(std::string const& filename, offset_t s_off, offset_t e_off)
345  : FileCollection(filename)
346  , m_vs(s_off, e_off)
347 {
348  std::ifstream zipfile(m_filename, std::ios::in | std::ios::binary);
349  if(!zipfile)
350  {
351  throw IOException("Error opening Zip archive file for reading in binary mode.");
352  }
353 
354  // Find and read the End of Central Directory.
356  {
357  BackBuffer bb(zipfile, m_vs);
358  ssize_t read_p(-1);
359  for(;;)
360  {
361  if(read_p < 0)
362  {
363  if(!bb.readChunk(read_p))
364  {
365  throw FileCollectionException("Unable to find zip structure: End-of-central-directory");
366  }
367  }
368  // Note: this is pretty fast since it reads from 'bb' which
369  // caches the buffer the readChunk() function just read.
370  //
371  if(eocd.read(bb, read_p))
372  {
373  // found it!
374  break;
375  }
376  --read_p;
377  }
378  }
379 
380  // Position read pointer to start of first entry in central dir.
381  m_vs.vseekg(zipfile, eocd.getOffset(), std::ios::beg);
382 
383  // TBD -- is that ", 0" still necessary? (With VC2012 and better)
384  // Give the second argument in the next line to keep Visual C++ quiet
385  //m_entries.resize(eocd.totalCount(), 0);
386  m_entries.resize(eocd.getCount());
387 
388  size_t const max_entry(eocd.getCount());
389  for(size_t entry_num(0); entry_num < max_entry; ++entry_num)
390  {
392  m_entries[entry_num].get()->read(zipfile);
393  }
394 
395  // Consistency check #1:
396  // The virtual seeker position is exactly the start offset of the
397  // Central Directory plus the Central Directory size
398  //
399  offset_t const pos(m_vs.vtellg(zipfile));
400  if(static_cast<offset_t>(eocd.getOffset() + eocd.getCentralDirectorySize()) != pos)
401  {
402  throw FileCollectionException("Zip file consistency problem. Zip file data fields are inconsistent with zip file layout.");
403  }
404 
405  // Consistency check #2:
406  // Are local headers consistent with CD headers?
407  //
408  for(auto it = m_entries.begin(); it != m_entries.end(); ++it)
409  {
416  m_vs.vseekg(zipfile, (*it)->getEntryOffset(), std::ios::beg);
417  ZipLocalEntry zlh;
418  zlh.read(zipfile);
419  if(!zipfile || !zlh.isEqual(**it))
420  {
421  throw FileCollectionException("Zip file consistency problem. Zip file data fields are inconsistent with zip file layout.");
422  }
423  }
424 
425  // we are all good!
426  m_valid = true;
427 }
428 
429 
437 {
438  return FileCollection::pointer_t(new ZipFile(*this));
439 }
440 
441 
448 {
449  close();
450 }
451 
452 
487 ZipFile::stream_pointer_t ZipFile::getInputStream(std::string const& entry_name, MatchPath matchpath)
488 {
489  mustBeValid();
490 
491  FileEntry::pointer_t entry(getEntry(entry_name, matchpath));
492  if(entry)
493  {
494  stream_pointer_t zis(new ZipInputStream(m_filename, entry->getEntryOffset() + m_vs.startOffset()));
495  return zis;
496  }
497 
498  // no entry with that name (and match) available
499  return nullptr;
500 }
501 
502 
512 void ZipFile::saveCollectionToArchive(std::ostream & os, FileCollection & collection, std::string const & zip_comment)
513 {
514  try
515  {
516  ZipOutputStream output_stream(os);
517 
518  output_stream.setComment(zip_comment);
519 
520  FileEntry::vector_t entries(collection.entries());
521  for(auto it(entries.begin()); it != entries.end(); ++it)
522  {
523  output_stream.putNextEntry(*it);
524  // get an InputStream if available (i.e. directories do not have an input stream)
525  if(!(*it)->isDirectory())
526  {
527  FileCollection::stream_pointer_t is(collection.getInputStream((*it)->getName()));
528  if(is)
529  {
530  output_stream << is->rdbuf();
531  }
532  }
533  }
534 
535  // clean up mantually so we can get any exception
536  // (so we avoid having exceptions gobbled by the destructor)
537  output_stream.closeEntry();
538  output_stream.finish();
539  output_stream.close();
540  }
541  catch(...)
542  {
543  os.setstate(std::ios::failbit);
544  throw;
545  }
546 }
547 
548 
549 } // zipios namespace
550 
551 // Local Variables:
552 // mode: cpp
553 // indent-tabs-mode: nil
554 // c-basic-offset: 4
555 // tab-width: 4
556 // End:
557 
558 // vim: ts=4 sw=4 et
zipios::FileCollection::entries
virtual FileEntry::vector_t entries() const
Retrieve the array of entries.
Definition: filecollection.cpp:396
zipios::ZipFile::clone
virtual pointer_t clone() const override
Create a clone of this ZipFile.
Definition: zipfile.cpp:436
zipios::FileCollection::getEntry
virtual FileEntry::pointer_t getEntry(std::string const &name, MatchPath matchpath=MatchPath::MATCH) const
Get an entry from this collection.
Definition: filecollection.cpp:424
zipios::ZipOutputStream::putNextEntry
void putNextEntry(FileEntry::pointer_t entry)
Add an entry to the output stream.
Definition: zipoutputstream.cpp:129
zipios::FileCollection::m_valid
bool m_valid
Definition: filecollection.hpp:74
zipios::ZipFile::saveCollectionToArchive
static void saveCollectionToArchive(std::ostream &os, FileCollection &collection, std::string const &zip_comment="")
Create a Zip archive from the specified FileCollection.
Definition: zipfile.cpp:512
zipios::ZipCentralDirectoryEntry
A specialization of ZipLocalEntry for.
Definition: zipcentraldirectoryentry.hpp:44
zipios::FileCollection::stream_pointer_t
std::shared_ptr< std::istream > stream_pointer_t
A shared pointer to an input stream.
Definition: filecollection.hpp:45
zipios::BackBuffer
To read a file by chunk from the end.
Definition: backbuffer.hpp:42
zipios::ZipEndOfCentralDirectory::getCentralDirectorySize
size_t getCentralDirectorySize() const
Retrieve the size of the Central Directory in bytes.
Definition: zipendofcentraldirectory.cpp:102
zipios::ZipOutputStream::close
void close()
Close the current stream.
Definition: zipoutputstream.cpp:90
zipfile.hpp
Define the zipios::ZipFile class.
zipios::FileCollection::mustBeValid
virtual void mustBeValid() const
Check whether the collection is valid.
Definition: filecollection.cpp:509
zipios::ZipOutputStream::finish
void finish()
Finish up the output by flushing anything left.
Definition: zipoutputstream.cpp:103
zipios::ZipInputStream
The ZipInputStream to read data from a Zip archive.
Definition: zipinputstream.hpp:44
zipiosexceptions.hpp
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
zipios::VirtualSeeker::vseekg
void vseekg(std::istream &is, offset_t offset, std::ios::seekdir sd) const
Seek within the embedded file.
Definition: virtualseeker.cpp:178
zipios::BackBuffer::readChunk
ssize_t readChunk(ssize_t &read_pointer)
Read a chunk of data.
Definition: backbuffer.cpp:141
zipios::ZipOutputStream::setComment
void setComment(std::string const &comment)
Set the global comment.
Definition: zipoutputstream.cpp:154
zipios::FileEntry::pointer_t
std::shared_ptr< FileEntry > pointer_t
Definition: fileentry.hpp:78
zipoutputstream.hpp
Define the zipios::ZipOutputStream class.
zipios::FileCollection::getInputStream
virtual stream_pointer_t getInputStream(std::string const &entry_name, MatchPath matchpath=MatchPath::MATCH)=0
Retrieve pointer to an istream.
zipios::ZipFile::ZipFile
ZipFile()
Initialize a ZipFile object.
Definition: zipfile.cpp:320
zipios::ZipOutputStream::closeEntry
void closeEntry()
Definition: zipoutputstream.cpp:76
zipios::ZipEndOfCentralDirectory::getCount
size_t getCount() const
Retrieve the number of entries.
Definition: zipendofcentraldirectory.cpp:121
zipios::IOException
An IOException is used to signal an I/O error.
Definition: zipiosexceptions.hpp:71
zipinputstream.hpp
Define zipios::ZipInputStream.
zipios::VirtualSeeker::vtellg
std::streampos vtellg(std::istream &is) const
Current position within the sub-file.
Definition: virtualseeker.cpp:218
zipcentraldirectoryentry.hpp
Declaration of the zipios::ZipCentralDirectoryEntry, which represents a directory Zip archive entry.
zipios::ZipFile::m_vs
VirtualSeeker m_vs
Definition: zipfile.hpp:61
zipios::FileCollection
Base class for various file collections.
Definition: filecollection.hpp:41
zipios::ZipFile::openEmbeddedZipFile
static pointer_t openEmbeddedZipFile(std::string const &name)
Open a zip archive that was previously appended to another file.
Definition: zipfile.cpp:297
zipios::ZipFile::~ZipFile
virtual ~ZipFile() override
Clean up the ZipFile object.
Definition: zipfile.cpp:447
zipios::FileCollection::MatchPath
MatchPath
Definition: filecollection.hpp:48
zipios::FileCollectionException
FileCollectionException is used to signal a FileCollection problem.
Definition: zipiosexceptions.hpp:95
zipios::FileCollection::close
virtual void close()
Close the current FileEntry of this FileCollection.
Definition: filecollection.cpp:378
zipios::ZipLocalEntry
An implementation of the FileEntry for Zip archives.
Definition: ziplocalentry.hpp:43
zipios::ZipLocalEntry::read
virtual void read(std::istream &is) override
Read one local entry from is.
Definition: ziplocalentry.cpp:335
zipios::ZipOutputStream
A ZipOutputStream to allow for data to be compressed zlib.
Definition: zipoutputstream.hpp:42
zipios::VirtualSeeker::startOffset
offset_t startOffset() const
Return the start offset.
Definition: virtualseeker.cpp:149
zipios::ZipLocalEntry::isEqual
virtual bool isEqual(FileEntry const &file_entry) const override
Compare two file entries for equality.
Definition: ziplocalentry.cpp:208
zipios::ZipEndOfCentralDirectory::read
bool read(::zipios::buffer_t const &buf, size_t pos)
Attempt to read an ZipEndOfCentralDirectory structure.
Definition: zipendofcentraldirectory.cpp:228
zipios::ZipFile::getInputStream
virtual stream_pointer_t getInputStream(std::string const &entry_name, MatchPath matchpath=MatchPath::MATCH) override
Retrieve a pointer to a file in the Zip archive.
Definition: zipfile.cpp:487
zipios::FileCollection::m_entries
FileEntry::vector_t m_entries
Definition: filecollection.hpp:73
zipios::zipRead
void zipRead(std::istream &is, uint32_t &value)
Definition: zipios_common.cpp:74
backbuffer.hpp
The header file for zipios::BackBuffer.
zipios::FileCollection::pointer_t
std::shared_ptr< FileCollection > pointer_t
Definition: filecollection.hpp:43
zipios::FileCollection::m_filename
std::string m_filename
Definition: filecollection.hpp:72
zipios::offset_t
std::streamoff offset_t
Definition: zipios-config.hpp.in:59
zipios::ZipEndOfCentralDirectory
Marker at the end of a Zip archive file.
Definition: zipendofcentraldirectory.hpp:46
zipendofcentraldirectory.hpp
Declaration of the zipios::ZipEndOfCentralDirectory class.
zipios::ZipEndOfCentralDirectory::getOffset
offset_t getOffset() const
Retrieve the offset of the Central Directory.
Definition: zipendofcentraldirectory.cpp:142
zipios::FileEntry::vector_t
std::vector< pointer_t > vector_t
Definition: fileentry.hpp:79
zipios
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:36