zipios 2.3.4
Zipios -- a small C++ library providing 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-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
32#include "zipios/fileentry.hpp"
33
35
36#include "zipios_common.hpp"
37
38
39namespace zipios
40{
41
99FileEntry::FileEntry(FilePath const & filename, std::string const & comment)
100 : m_filename(filename)
101 , m_comment(comment)
102{
103}
104
105
128
129
141std::string FileEntry::getComment() const
142{
143 return m_comment;
144}
145
146
156{
157 return getSize();
158}
159
160
161
162
174{
178 return m_crc_32;
179}
180
181
193std::streampos FileEntry::getEntryOffset() const
194{
195 return m_entry_offset;
196}
197
198
217
218
229{
230 return 0;
231}
232
233
254{
255 if(isDirectory())
256 {
258 }
259 return m_compression_level;
260}
261
262
274{
275 if(isDirectory())
276 {
277 // make sure we do not return anything else than STORED
278 // for a directory
280 }
281 return m_compress_method;
282}
283
284
285
286
294std::string FileEntry::getName() const
295{
296 return m_filename;
297}
298
299
311std::string FileEntry::getFileName() const
312{
313 return m_filename.filename();
314}
315
316
323std::size_t FileEntry::getSize() const
324{
325 return m_uncompressed_size;
326}
327
328
345{
346 if(m_unix_time == 0)
347 {
348 return 0;
349 }
350
351 DOSDateTime t;
353 return t.getDOSDateTime();
354}
355
356
376std::time_t FileEntry::getUnixTime() const
377{
378 return m_unix_time;
379}
380
381
391{
392 return m_has_crc_32;
393}
394
395
405{
406 return m_filename.isDirectory();
407}
408
409
430bool FileEntry::isEqual(FileEntry const & file_entry) const
431{
432 return m_filename == file_entry.m_filename
433 && m_comment == file_entry.m_comment
435 && m_unix_time == file_entry.m_unix_time
436 && m_compress_method == file_entry.m_compress_method
437 && m_crc_32 == file_entry.m_crc_32
438 && m_has_crc_32 == file_entry.m_has_crc_32
439 && m_valid == file_entry.m_valid;
440 //&& m_extra_field == file_entry.m_extra_field -- ignored in comparison
441}
442
443
453{
454 return m_valid;
455}
456
457
466void FileEntry::setComment(std::string const & comment)
467{
468 // WARNING: we do NOT check the maximum size here because it can depend
469 // on the output format which is just zip now but could be a
470 // bit extended later (i.e. Zip64)
471 m_comment = comment;
472}
473
474
486{
487 static_cast<void>(size);
488}
489
490
498{
499 static_cast<void>(crc);
500}
501
502
517void FileEntry::setEntryOffset(std::streampos offset)
518{
519 m_entry_offset = offset;
520}
521
522
532void FileEntry::setExtra(buffer_t const & extra)
533{
534 m_extra_field = extra;
535}
536
537
554{
555 if(level < COMPRESSION_LEVEL_DEFAULT || level > COMPRESSION_LEVEL_MAXIMUM)
556 {
557 throw InvalidStateException("level must be between COMPRESSION_LEVEL_DEFAULT and COMPRESSION_LEVEL_MAXIMUM inclusive");
558 }
559 if(isDirectory())
560 {
561 if(level >= COMPRESSION_LEVEL_MINIMUM)
562 {
563 throw InvalidStateException("directories cannot be marked with a compression level other than COMPRESSION_LEVEL_NONE (defaults will also work");
564 }
566 }
567 else
568 {
569 m_compression_level = level;
570 }
571}
572
573
591{
592 switch(method)
593 {
595 //case StorageMethod::SHRUNK:
596 //case StorageMethod::REDUCED1:
597 //case StorageMethod::REDUCED2:
598 //case StorageMethod::REDUCED3:
599 //case StorageMethod::REDUCED4:
600 //case StorageMethod::IMPLODED:
601 //case StorageMethod::TOKENIZED:
603 //case StorageMethod::DEFLATED64:
604 //case StorageMethod::OLD_TERSE:
605 //case StorageMethod::RESERVED11:
606 //case StorageMethod::BZIP2:
607 //case StorageMethod::REVERVED13:
608 //case StorageMethod::LZMA:
609 //case StorageMethod::RESERVED15:
610 //case StorageMethod::RESERVED16:
611 //case StorageMethod::RESERVED17:
612 //case StorageMethod::NEW_TERSE:
613 //case StorageMethod::LZ77:
614 //case StorageMethod::WAVPACK:
615 //case StorageMethod::PPMD_I_1:
616 break;
617
618 default:
619 throw InvalidStateException("unknown method");
620
621 }
622
623 if(isDirectory())
624 {
625 // never compress directories
627 }
628 else
629 {
630 m_compress_method = method;
631 }
632}
633
634
642void FileEntry::setSize(size_t size)
643{
644 m_uncompressed_size = size;
645}
646
647
659{
660 DOSDateTime t;
661 t.setDOSDateTime(dosdatetime);
663}
664
665
675void FileEntry::setUnixTime(std::time_t time)
676{
677 m_unix_time = time;
678}
679
680
690std::string FileEntry::toString() const
691{
693 sout << m_filename;
694 if(isDirectory())
695 {
696 sout << " (directory)";
697 }
698 else
699 {
700 sout << " ("
701 << m_uncompressed_size << " byte"
702 << (m_uncompressed_size == 1 ? "" : "s");
703 size_t const compressed_size(getCompressedSize());
704 if(compressed_size != m_uncompressed_size)
705 {
706 // this is not currently accessible since only the
707 // ZipLocalEntry and ZipCentralDirectoryEntry have
708 // a compressed size
709 sout << ", " // LCOV_EXCL_LINE
710 << compressed_size << " byte" // LCOV_EXCL_LINE
711 << (compressed_size == 1 ? "" : "s") // LCOV_EXCL_LINE
712 << " compressed"; // LCOV_EXCL_LINE
713 }
714 sout << ")";
715 }
716 return sout.str();
717}
718
719
731void FileEntry::read(std::istream & is)
732{
733 static_cast<void>(is);
734 throw IOException("FileEntry::read(): read not available with this type of FileEntry.");
735}
736
737
749void FileEntry::write(std::ostream & os)
750{
751 static_cast<void>(os);
752 throw IOException("FileEntry::write(): write not available with this type of FileEntry.");
753}
754
755
768std::ostream & operator << (std::ostream & os, FileEntry const & entry)
769{
770 os << entry.toString();
771 return os;
772}
773
774
775} // namespace
776
777// Local Variables:
778// mode: cpp
779// indent-tabs-mode: nil
780// c-basic-offset: 4
781// tab-width: 4
782// End:
783
784// 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
std::size_t m_uncompressed_size
StorageMethod m_compress_method
virtual std::string getFileName() const
Return the basename of this entry.
virtual std::size_t getSize() const
Retrieve the size of the file when uncompressed.
std::string m_comment
int CompressionLevel
The compression level to be used to save an entry.
Definition fileentry.hpp:85
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 std::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.
virtual void setLevel(CompressionLevel level)
Define the level of compression to use by this FileEntry.
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 std::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
bool isDirectory() const
Check whether the file is a directory.
Definition filepath.cpp:422
std::string filename() const
Retrieve the basename.
Definition filepath.cpp:315
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.