zipios 2.2.0
Zipios -- a small C++ library that provides easy access to .zip files.
fileentry.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
31
32#include "zipios/fileentry.hpp"
33
35
36#include "zipios_common.hpp"
37
38
39namespace zipios
40{
41
52
53
66
67
85
86
99FileEntry::FileEntry(FilePath const& filename, std::string const& comment)
100 : m_filename(filename)
101 , m_comment(comment)
102 //, m_uncompressed_size(0) -- auto-init
103 //, m_unix_time(0) -- auto-init
104 //, m_entry_offset(0) -- auto-init
105 //, m_compress_method(StorageMethod::STORED) -- auto-init
106 //, m_compression_level(COMPRESSION_LEVEL_DEFAULT) -- auto-init
107 //, m_crc_32(0) -- auto-init
108 //, m_has_crc_32(false) -- auto-init
109 //, m_valid(false) -- auto-init
110{
111}
112
113
125
126
136
137
149std::string FileEntry::getComment() const
150{
151 return m_comment;
152}
153
154
164{
165 return getSize();
166}
167
168
169
170
182{
186 return m_crc_32;
187}
188
189
201std::streampos FileEntry::getEntryOffset() const
202{
203 return m_entry_offset;
204}
205
206
225
226
237{
238 return 0;
239}
240
241
262{
263 if(isDirectory())
264 {
266 }
267 return m_compression_level;
268}
269
270
282{
283 if(isDirectory())
284 {
285 // make sure we do not return anything else than STORED
286 // for a directory
288 }
289 return m_compress_method;
290}
291
292
293
294
302std::string FileEntry::getName() const
303{
304 return m_filename;
305}
306
307
319std::string FileEntry::getFileName() const
320{
321 return m_filename.filename();
322}
323
324
331size_t FileEntry::getSize() const
332{
333 return m_uncompressed_size;
334}
335
336
349{
350 if(m_unix_time == 0)
351 {
352 return 0;
353 }
354
355 DOSDateTime t;
357 return t.getDOSDateTime();
358}
359
360
380std::time_t FileEntry::getUnixTime() const
381{
382 return m_unix_time;
383}
384
385
395{
396 return m_has_crc_32;
397}
398
399
409{
410 return m_filename.isDirectory();
411}
412
413
431bool FileEntry::isEqual(FileEntry const & file_entry) const
432{
433 return m_filename == file_entry.m_filename
434 && m_comment == file_entry.m_comment
436 && m_unix_time == file_entry.m_unix_time
437 && m_compress_method == file_entry.m_compress_method
438 && m_crc_32 == file_entry.m_crc_32
439 && m_has_crc_32 == file_entry.m_has_crc_32
440 && m_valid == file_entry.m_valid;
441 //&& m_extra_field == file_entry.m_extra_field -- ignored in comparison
442}
443
444
454{
455 return m_valid;
456}
457
458
467void FileEntry::setComment(std::string const& comment)
468{
469 // WARNING: we do NOT check the maximum size here because it can depend
470 // on the output format which is just zip now but could be a
471 // bit extended later (i.e. Zip64)
472 m_comment = comment;
473}
474
475
487{
488 static_cast<void>(size);
489}
490
491
499{
500 static_cast<void>(crc);
501}
502
503
518void FileEntry::setEntryOffset(std::streampos offset)
519{
520 m_entry_offset = offset;
521}
522
523
534{
535 m_extra_field = extra;
536}
537
538
555{
556 if(level < COMPRESSION_LEVEL_DEFAULT || level > COMPRESSION_LEVEL_MAXIMUM)
557 {
558 throw InvalidStateException("level must be between COMPRESSION_LEVEL_DEFAULT and COMPRESSION_LEVEL_MAXIMUM");
559 }
560 if(isDirectory())
561 {
562 if(level >= COMPRESSION_LEVEL_MINIMUM)
563 {
564 throw InvalidStateException("directories cannot be marked with a compression level other than COMPRESSION_LEVEL_NONE (defaults will also work");
565 }
567 }
568 else
569 {
570 m_compression_level = level;
571 }
572}
573
574
592{
593 switch(method)
594 {
596 //case StorageMethod::SHRUNK:
597 //case StorageMethod::REDUCED1:
598 //case StorageMethod::REDUCED2:
599 //case StorageMethod::REDUCED3:
600 //case StorageMethod::REDUCED4:
601 //case StorageMethod::IMPLODED:
602 //case StorageMethod::TOKENIZED:
604 //case StorageMethod::DEFLATED64:
605 //case StorageMethod::OLD_TERSE:
606 //case StorageMethod::RESERVED11:
607 //case StorageMethod::BZIP2:
608 //case StorageMethod::REVERVED13:
609 //case StorageMethod::LZMA:
610 //case StorageMethod::RESERVED15:
611 //case StorageMethod::RESERVED16:
612 //case StorageMethod::RESERVED17:
613 //case StorageMethod::NEW_TERSE:
614 //case StorageMethod::LZ77:
615 //case StorageMethod::WAVPACK:
616 //case StorageMethod::PPMD_I_1:
617 break;
618
619 default:
620 throw InvalidStateException("unknown method");
621
622 }
623
624 if(isDirectory())
625 {
626 // force uncompressed for directories
628 }
629 else
630 {
631 m_compress_method = method;
632 }
633}
634
635
643void FileEntry::setSize(size_t size)
644{
645 m_uncompressed_size = size;
646}
647
648
660{
661 DOSDateTime t;
662 t.setDOSDateTime(dosdatetime);
664}
665
666
676void FileEntry::setUnixTime(std::time_t time)
677{
678 m_unix_time = time;
679}
680
681
691std::string FileEntry::toString() const
692{
694 sout << m_filename;
695 if(isDirectory())
696 {
697 sout << " (directory)";
698 }
699 else
700 {
701 sout << " ("
702 << m_uncompressed_size << " byte"
703 << (m_uncompressed_size == 1 ? "" : "s");
704 size_t const compressed_size(getCompressedSize());
705 if(compressed_size != m_uncompressed_size)
706 {
707 // this is not currently accessible since only the
708 // ZipLocalEntry and ZipCentralDirectoryEntry have
709 // a compressed size
710 sout << ", " // LCOV_EXCL_LINE
711 << compressed_size << " byte" // LCOV_EXCL_LINE
712 << (compressed_size == 1 ? "" : "s") // LCOV_EXCL_LINE
713 << " compressed"; // LCOV_EXCL_LINE
714 }
715 sout << ")";
716 }
717 return sout.str();
718}
719
720
732void FileEntry::read(std::istream& is)
733{
734 static_cast<void>(is);
735 throw IOException("FileEntry::read(): read not available with this type of FileEntry.");
736}
737
738
750void FileEntry::write(std::ostream& os)
751{
752 static_cast<void>(os);
753 throw IOException("FileEntry::write(): write not available with this type of FileEntry.");
754}
755
756
767std::ostream& operator << (std::ostream& os, FileEntry const& entry)
768{
769 os << entry.toString();
770 return os;
771}
772
773
774} // namespace
775
776// Local Variables:
777// mode: cpp
778// indent-tabs-mode: nil
779// c-basic-offset: 4
780// tab-width: 4
781// End:
782
783// vim: ts=4 sw=4 et
dosdatetime_t getDOSDateTime() const
Retrieve the DOSDateTime value as is.
std::time_t getUnixTimestamp() const
Retrieve the DOSDateTime as a Unix timestamp.
void setDOSDateTime(dosdatetime_t datetime)
Set the DOSDateTime value as is.
void setUnixTimestamp(std::time_t unix_timestamp)
Set the DOSDateTime value from a Unix timestamp.
A FileEntry represents an entry in a FileCollection.
Definition fileentry.hpp:76
std::vector< unsigned char > buffer_t
Definition fileentry.hpp:80
virtual size_t getSize() const
Retrieve the size of the file when uncompressed.
StorageMethod m_compress_method
virtual std::string getFileName() const
Return the basename of this entry.
std::string m_comment
static CompressionLevel const COMPRESSION_LEVEL_MINIMUM
Definition fileentry.hpp:91
virtual void setComment(std::string const &comment)
Set the comment field for the FileEntry.
buffer_t m_extra_field
virtual size_t getCompressedSize() const
Retrieve the size of the file when compressed.
static CompressionLevel const COMPRESSION_LEVEL_MAXIMUM
Definition fileentry.hpp:92
virtual std::string getComment() const
Retrieve the comment of the file entry.
bool hasCrc() const
Check whether the CRC32 was defined.
virtual void setTime(DOSDateTime::dosdatetime_t time)
Set the FileEntry time using a DOS time.
virtual std::string getName() const
Return the filename of the entry.
virtual bool isValid() const
Check whether this entry is valid.
virtual void setSize(size_t size)
Sets the size field for the entry.
virtual void setUnixTime(std::time_t time)
Sets the time field in Unix time format for the entry.
virtual void setExtra(buffer_t const &extra)
Set the extra field buffer.
virtual void read(std::istream &is)
Read this FileEntry from the input stream.
int CompressionLevel
The compression level to be used to save an entry.
Definition fileentry.hpp:85
virtual void setLevel(CompressionLevel level)
Define the level of compression to use by this FileEntry.
size_t m_uncompressed_size
virtual bool isDirectory() const
Check whether the filename represents a directory.
CompressionLevel m_compression_level
std::streampos m_entry_offset
static CompressionLevel const COMPRESSION_LEVEL_NONE
Definition fileentry.hpp:90
virtual void setCrc(crc32_t crc)
Save the CRC of the entry.
virtual StorageMethod getMethod() const
Return the method used to create this entry.
virtual CompressionLevel getLevel() const
Retrieve the compression level.
virtual std::string toString() const
Returns a human-readable string representation of the entry.
virtual crc32_t getCrc() const
Return the CRC of the entry.
virtual void setCompressedSize(size_t size)
Set the size when the file is compressed.
virtual buffer_t getExtra() const
Some extra data to be stored along the entry.
virtual DOSDateTime::dosdatetime_t getTime() const
Get the MS-DOS date/time of this entry.
std::streampos getEntryOffset() const
Get the offset of this entry in a Zip archive.
virtual std::time_t getUnixTime() const
Get the Unix date/time of this entry.
virtual bool isEqual(FileEntry const &file_entry) const
Compare two file entries for equality.
virtual void write(std::ostream &os)
Write this FileEntry to the output stream.
virtual void setMethod(StorageMethod method)
Sets the storage method field for the entry.
FileEntry(FilePath const &filename, std::string const &comment=std::string())
Initialize a FileEntry object.
Definition fileentry.cpp:99
virtual size_t getHeaderSize() const
Retrieve the size of the header.
virtual ~FileEntry()
Clean up a FileEntry object.
void setEntryOffset(std::streampos offset)
Defines the position of the entry in a Zip archive.
Handle a file path and name and its statistics.
Definition filepath.hpp:47
An IOException is used to signal an I/O error.
Exception used when it is not possible to move forward.
Define the zipios::FileEntry class.
The zipios namespace includes the Zipios library definitions.
std::ostream & operator<<(std::ostream &os, FileCollection const &collection)
Write a FileCollection to the output stream.
StorageMethod
The types used with FileEntry::setMethod and FileEntry::getMethod.
Definition fileentry.hpp:49
std::ostringstream OutputStringStream
An output stream using strings.
Various functions used throughout the library.
Various exceptions used throughout the Zipios library, all based on zipios::Exception.