zipios 2.2.0
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
53namespace zipios
54{
55
56
307{
308 // open zipfile, read 4 last bytes close file
309 // create ZipFile object.
310 uint32_t start_offset;
311 {
312 std::ifstream ifs(name, std::ios::in | std::ios::binary);
313 ifs.seekg(-4, std::ios::end);
314 zipRead(ifs, start_offset);
315 // todo: add support for 64 bit (files of more than 4Gb)
316 }
317 return ZipFile::pointer_t(new ZipFile(name, start_offset, 4));
318}
319
320
330 //: m_vs(...) -- auto-init
331{
332}
333
334
353ZipFile::ZipFile(std::string const& filename, offset_t s_off, offset_t e_off)
354 : FileCollection(filename)
355 , m_vs(s_off, e_off)
356{
357 std::ifstream zipfile(m_filename, std::ios::in | std::ios::binary);
358 if(!zipfile)
359 {
360 throw IOException("Error opening Zip archive file for reading in binary mode.");
361 }
362
363 // Find and read the End of Central Directory.
365 {
366 BackBuffer bb(zipfile, m_vs);
367 ssize_t read_p(-1);
368 for(;;)
369 {
370 if(read_p < 0)
371 {
372 if(!bb.readChunk(read_p))
373 {
374 throw FileCollectionException("Unable to find zip structure: End-of-central-directory");
375 }
376 }
377 // Note: this is pretty fast since it reads from 'bb' which
378 // caches the buffer the readChunk() function just read.
379 //
380 if(eocd.read(bb, read_p))
381 {
382 // found it!
383 break;
384 }
385 --read_p;
386 }
387 }
388
389 // Position read pointer to start of first entry in central dir.
390 m_vs.vseekg(zipfile, eocd.getOffset(), std::ios::beg);
391
392 // TBD -- is that ", 0" still necessary? (With VC2012 and better)
393 // Give the second argument in the next line to keep Visual C++ quiet
394 //m_entries.resize(eocd.totalCount(), 0);
395 m_entries.resize(eocd.getCount());
396
397 size_t const max_entry(eocd.getCount());
398 for(size_t entry_num(0); entry_num < max_entry; ++entry_num)
399 {
401 m_entries[entry_num].get()->read(zipfile);
402 }
403
404 // Consistency check #1:
405 // The virtual seeker position is exactly the start offset of the
406 // Central Directory plus the Central Directory size
407 //
408 offset_t const pos(m_vs.vtellg(zipfile));
409 if(static_cast<offset_t>(eocd.getOffset() + eocd.getCentralDirectorySize()) != pos)
410 {
411 throw FileCollectionException("Zip file consistency problem. Zip file data fields are inconsistent with zip file layout.");
412 }
413
414 // Consistency check #2:
415 // Are local headers consistent with CD headers?
416 //
417 for(auto it = m_entries.begin(); it != m_entries.end(); ++it)
418 {
425 m_vs.vseekg(zipfile, (*it)->getEntryOffset(), std::ios::beg);
426 ZipLocalEntry zlh;
427 zlh.read(zipfile);
428 if(!zipfile || !zlh.isEqual(**it))
429 {
430 throw FileCollectionException("Zip file consistency problem. Zip file data fields are inconsistent with zip file layout.");
431 }
432 }
433
434 // we are all good!
435 m_valid = true;
436}
437
438
449
450
457{
458 close();
459}
460
461
496ZipFile::stream_pointer_t ZipFile::getInputStream(std::string const& entry_name, MatchPath matchpath)
497{
498 mustBeValid();
499
500 FileEntry::pointer_t entry(getEntry(entry_name, matchpath));
501 if(entry)
502 {
503 stream_pointer_t zis(new ZipInputStream(m_filename, entry->getEntryOffset() + m_vs.startOffset()));
504 return zis;
505 }
506
507 // no entry with that name (and match) available
508 return nullptr;
509}
510
511
521void ZipFile::saveCollectionToArchive(std::ostream & os, FileCollection & collection, std::string const & zip_comment)
522{
523 try
524 {
525 ZipOutputStream output_stream(os);
526
527 output_stream.setComment(zip_comment);
528
529 FileEntry::vector_t entries(collection.entries());
530 for(auto it(entries.begin()); it != entries.end(); ++it)
531 {
532 output_stream.putNextEntry(*it);
533 // get an InputStream if available (i.e. directories do not have an input stream)
534 if(!(*it)->isDirectory())
535 {
536 FileCollection::stream_pointer_t is(collection.getInputStream((*it)->getName()));
537 if(is)
538 {
539 output_stream << is->rdbuf();
540 }
541 }
542 }
543
544 // clean up mantually so we can get any exception
545 // (so we avoid having exceptions gobbled by the destructor)
546 output_stream.closeEntry();
547 output_stream.finish();
548 output_stream.close();
549 }
550 catch(...)
551 {
552 os.setstate(std::ios::failbit);
553 throw;
554 }
555}
556
557
558} // zipios namespace
559
560// Local Variables:
561// mode: cpp
562// indent-tabs-mode: nil
563// c-basic-offset: 4
564// tab-width: 4
565// End:
566
567// vim: ts=4 sw=4 et
The header file for zipios::BackBuffer.
To read a file by chunk from the end.
ssize_t readChunk(ssize_t &read_pointer)
Read a chunk of data.
FileCollectionException is used to signal a FileCollection problem.
Base class for various file collections.
virtual FileEntry::pointer_t getEntry(std::string const &name, MatchPath matchpath=MatchPath::MATCH) const
Get an entry from this collection.
std::shared_ptr< FileCollection > pointer_t
std::shared_ptr< std::istream > stream_pointer_t
A shared pointer to an input stream.
virtual void mustBeValid() const
Check whether the collection is valid.
virtual void close()
Close the current FileEntry of this FileCollection.
FileEntry::vector_t m_entries
virtual stream_pointer_t getInputStream(std::string const &entry_name, MatchPath matchpath=MatchPath::MATCH)=0
Retrieve pointer to an istream.
virtual FileEntry::vector_t entries() const
Retrieve the array of entries.
std::shared_ptr< FileEntry > pointer_t
Definition fileentry.hpp:78
std::vector< pointer_t > vector_t
Definition fileentry.hpp:79
An IOException is used to signal an I/O error.
offset_t startOffset() const
Return the start offset.
void vseekg(std::istream &is, offset_t offset, std::ios::seekdir sd) const
Seek within the embedded file.
std::streampos vtellg(std::istream &is) const
Current position within the sub-file.
A specialization of ZipLocalEntry for.
Marker at the end of a Zip archive file.
size_t getCentralDirectorySize() const
Retrieve the size of the Central Directory in bytes.
size_t getCount() const
Retrieve the number of entries.
offset_t getOffset() const
Retrieve the offset of the Central Directory.
bool read(::zipios::buffer_t const &buf, size_t pos)
Attempt to read an ZipEndOfCentralDirectory structure.
static void saveCollectionToArchive(std::ostream &os, FileCollection &collection, std::string const &zip_comment="")
Create a Zip archive from the specified FileCollection.
Definition zipfile.cpp:521
ZipFile()
Initialize a ZipFile object.
Definition zipfile.cpp:329
static pointer_t openEmbeddedZipFile(std::string const &name)
Open a zip archive that was previously appended to another file.
Definition zipfile.cpp:306
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:496
virtual ~ZipFile() override
Clean up the ZipFile object.
Definition zipfile.cpp:456
virtual pointer_t clone() const override
Create a clone of this ZipFile.
Definition zipfile.cpp:445
VirtualSeeker m_vs
Definition zipfile.hpp:61
The ZipInputStream to read data from a Zip archive.
An implementation of the FileEntry for Zip archives.
virtual void read(std::istream &is) override
Read one local entry from is.
virtual bool isEqual(FileEntry const &file_entry) const override
Compare two file entries for equality.
A ZipOutputStream to allow for data to be compressed zlib.
void close()
Close the current stream.
void finish()
Finish up the output by flushing anything left.
void putNextEntry(FileEntry::pointer_t entry)
Add an entry to the output stream.
void setComment(std::string const &comment)
Set the global comment.
The zipios namespace includes the Zipios library definitions.
std::streamoff offset_t
void zipRead(std::istream &is, uint32_t &value)
Declaration of the zipios::ZipCentralDirectoryEntry, which represents a directory Zip archive entry.
Declaration of the zipios::ZipEndOfCentralDirectory class.
Define the zipios::ZipFile class.
Define zipios::ZipInputStream.
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
Define the zipios::ZipOutputStream class.