zipios 2.3.4
Zipios -- a small C++ library providing easy access to .zip files.
streamentry.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-2022 Made to Order Software Corp. All Rights Reserved
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
30
32
33#include "zipios_common.hpp"
34
35#include <fstream>
36#include <zlib.h>
37
38
39namespace zipios
40{
41
68 std::istream & is
69 , FilePath const & filename
70 , std::string const & comment)
71 : FileEntry(filename, comment)
72 , f_istream(is)
73{
74 m_valid = static_cast<bool>(is);
75 if(m_valid)
76 {
77 std::istream::pos_type const current(is.tellg());
78 m_uncompressed_size = is.seekg(0, std::ios::end).tellg();
79 is.seekg(current, std::ios::beg);
80
81 m_unix_time = time(nullptr);
82 }
83}
84
85
97{
98 return std::make_shared<StreamEntry>(*this);
99}
100
101
111
112
126bool StreamEntry::isEqual(FileEntry const & file_entry) const
127{
128 StreamEntry const * const se(dynamic_cast<StreamEntry const * const>(&file_entry));
129 if(se == nullptr)
130 {
131 return false;
132 }
133 return FileEntry::isEqual(file_entry);
134}
135
136
150{
151 uint32_t result(crc32(0L, Z_NULL, 0));
152
153 if(f_istream)
154 {
155 f_istream.seekg(0, std::ios::beg);
156 for(;;)
157 {
158 Bytef buf[64 * 1024];
159 f_istream.read(reinterpret_cast<char *>(buf), sizeof(buf));
160 if(f_istream.gcount() == 0)
161 {
162 break;
163 }
164 result = crc32(result, buf, f_istream.gcount());
165 }
166 }
167
168 return result;
169}
170
171
179std::istream & StreamEntry::getStream() const
180{
181 return f_istream;
182}
183
184
185} // zipios namespace
186
187// Local Variables:
188// mode: cpp
189// indent-tabs-mode: nil
190// c-basic-offset: 4
191// tab-width: 4
192// End:
193
194// vim: ts=4 sw=4 et
A FileEntry represents an entry in a FileCollection.
Definition fileentry.hpp:76
std::size_t m_uncompressed_size
std::shared_ptr< FileEntry > pointer_t
Definition fileentry.hpp:78
virtual bool isEqual(FileEntry const &file_entry) const
Compare two file entries for equality.
Handle a file path and name and its statistics.
Definition filepath.hpp:47
A file entry reading from a stream.
std::istream & f_istream
virtual ~StreamEntry() override
Clean up a StreamEntry object.
StreamEntry(std::istream &is, FilePath const &filename, std::string const &comment=std::string())
Initialize a StreamEntry object.
virtual FileEntry::pointer_t clone() const override
Create a copy of the StreamEntry.
std::istream & getStream() const
Retrieve a reference to the istream object.
virtual bool isEqual(FileEntry const &file_entry) const override
Compare two file entries for equality.
uint32_t computeCRC32() const
Compute the CRC32 of this file.
The zipios namespace includes the Zipios library definitions.
Define the zipios::StreamEntry class.
Various functions used throughout the library.
Various exceptions used throughout the Zipios library, all based on zipios::Exception.