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-2017 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
32#include "zipios/fileentry.hpp"
33
34#include "dostime.h"
36
37#include "zipios_common.hpp"
38
39
40namespace zipios
41{
42
100FileEntry::FileEntry(FilePath const& filename, std::string const& comment)
101 : m_filename(filename)
102 , m_comment(comment)
103 //, m_uncompressed_size(0) -- auto-init
104 //, m_unix_time(0) -- auto-init
105 //, m_entry_offset(0) -- auto-init
106 //, m_compress_method(StorageMethod::STORED) -- auto-init
107 //, m_compression_level(COMPRESSION_LEVEL_DEFAULT) -- auto-init
108 //, m_crc_32(0) -- auto-init
109 //, m_has_crc_32(false) -- auto-init
110 //, m_valid(false) -- auto-init
111{
112}
113
114
135{
136}
137
138
150std::string FileEntry::getComment() const
151{
152 return m_comment;
153}
154
155
165{
166 return getSize();
167}
168
169
170
171
183{
187 return m_crc_32;
188}
189
190
202std::streampos FileEntry::getEntryOffset() const
203{
204 return m_entry_offset;
205}
206
207
223{
224 return m_extra_field;
225}
226
227
238{
239 return 0;
240}
241
242
263{
264 if(isDirectory())
265 {
267 }
268 return m_compression_level;
269}
270
271
283{
284 if(isDirectory())
285 {
286 // make sure we do not return anything else than STORED
287 // for a directory
289 }
290 return m_compress_method;
291}
292
293
294
295
303std::string FileEntry::getName() const
304{
305 return m_filename;
306}
307
308
320std::string FileEntry::getFileName() const
321{
322 return m_filename.filename();
323}
324
325
332size_t FileEntry::getSize() const
333{
334 return m_uncompressed_size;
335}
336
337
350{
352}
353
354
374std::time_t FileEntry::getUnixTime() const
375{
376 return m_unix_time;
377}
378
379
389{
390 return m_has_crc_32;
391}
392
393
403{
404 return m_filename.isDirectory();
405}
406
407
425bool FileEntry::isEqual(FileEntry const & file_entry) const
426{
427 return m_filename == file_entry.m_filename
428 && m_comment == file_entry.m_comment
430 && m_unix_time == file_entry.m_unix_time
431 && m_compress_method == file_entry.m_compress_method
432 && m_crc_32 == file_entry.m_crc_32
433 && m_has_crc_32 == file_entry.m_has_crc_32
434 && m_valid == file_entry.m_valid;
435 //&& m_extra_field == file_entry.m_extra_field -- ignored in comparison
436}
437
438
448{
449 return m_valid;
450}
451
452
461void FileEntry::setComment(std::string const& comment)
462{
463 // WARNING: we do NOT check the maximum size here because it can depend
464 // on the output format which is just zip now but could be a
465 // bit extended later (i.e. Zip64)
466 m_comment = comment;
467}
468
469
481{
482 static_cast<void>(size);
483}
484
485
493{
494 static_cast<void>(crc);
495}
496
497
512void FileEntry::setEntryOffset(std::streampos offset)
513{
514 m_entry_offset = offset;
515}
516
517
528{
529 m_extra_field = extra;
530}
531
532
549{
550 if(level < COMPRESSION_LEVEL_DEFAULT || level > COMPRESSION_LEVEL_MAXIMUM)
551 {
552 throw InvalidStateException("level must be between COMPRESSION_LEVEL_DEFAULT and COMPRESSION_LEVEL_MAXIMUM");
553 }
554 if(isDirectory())
555 {
556 if(level >= COMPRESSION_LEVEL_MINIMUM)
557 {
558 throw InvalidStateException("directories cannot be marked with a compression level other than COMPRESSION_LEVEL_NONE (defaults will also work");
559 }
561 }
562 else
563 {
564 m_compression_level = level;
565 }
566}
567
568
586{
587 switch(method)
588 {
590 //case StorageMethod::SHRUNK:
591 //case StorageMethod::REDUCED1:
592 //case StorageMethod::REDUCED2:
593 //case StorageMethod::REDUCED3:
594 //case StorageMethod::REDUCED4:
595 //case StorageMethod::IMPLODED:
596 //case StorageMethod::TOKENIZED:
598 //case StorageMethod::DEFLATED64:
599 //case StorageMethod::OLD_TERSE:
600 //case StorageMethod::RESERVED11:
601 //case StorageMethod::BZIP2:
602 //case StorageMethod::REVERVED13:
603 //case StorageMethod::LZMA:
604 //case StorageMethod::RESERVED15:
605 //case StorageMethod::RESERVED16:
606 //case StorageMethod::RESERVED17:
607 //case StorageMethod::NEW_TERSE:
608 //case StorageMethod::LZ77:
609 //case StorageMethod::WAVPACK:
610 //case StorageMethod::PPMD_I_1:
611 break;
612
613 default:
614 throw InvalidStateException("unknown method");
615
616 }
617
618 if(isDirectory())
619 {
620 // force uncompressed for directories
622 }
623 else
624 {
625 m_compress_method = method;
626 }
627}
628
629
637void FileEntry::setSize(size_t size)
638{
639 m_uncompressed_size = size;
640}
641
642
654{
656}
657
658
668void FileEntry::setUnixTime(std::time_t time)
669{
670 m_unix_time = time;
671}
672
673
683std::string FileEntry::toString() const
684{
686 sout << m_filename;
687 if(isDirectory())
688 {
689 sout << " (directory)";
690 }
691 else
692 {
693 sout << " ("
694 << m_uncompressed_size << " byte"
695 << (m_uncompressed_size == 1 ? "" : "s");
696 size_t const compressed_size(getCompressedSize());
697 if(compressed_size != m_uncompressed_size)
698 {
699 // this is not currently accessible since only the
700 // ZipLocalEntry and ZipCentralDirectoryEntry have
701 // a compressed size
702 sout << ", " // LCOV_EXCL_LINE
703 << compressed_size << " byte" // LCOV_EXCL_LINE
704 << (compressed_size == 1 ? "" : "s") // LCOV_EXCL_LINE
705 << " compressed"; // LCOV_EXCL_LINE
706 }
707 sout << ")";
708 }
709 return sout.str();
710}
711
712
724void FileEntry::read(std::istream& is)
725{
726 static_cast<void>(is);
727 throw IOException("FileEntry::read(): read not available with this type of FileEntry.");
728}
729
730
742void FileEntry::write(std::ostream& os)
743{
744 static_cast<void>(os);
745 throw IOException("FileEntry::write(): write not available with this type of FileEntry.");
746}
747
748
759std::ostream& operator << (std::ostream& os, FileEntry const& entry)
760{
761 os << entry.toString();
762 return os;
763}
764
765
766} // namespace
767
768// Local Variables:
769// mode: cpp
770// indent-tabs-mode: nil
771// c-basic-offset: 4
772// tab-width: 4
773// End:
774
775// vim: ts=4 sw=4 et
A FileEntry represents an entry in a FileCollection.
Definition: fileentry.hpp:75
std::vector< unsigned char > buffer_t
Definition: fileentry.hpp:79
virtual size_t getSize() const
Retrieve the size of the file when uncompressed.
Definition: fileentry.cpp:332
uint32_t dostime_t
Definition: fileentry.hpp:81
StorageMethod m_compress_method
Definition: fileentry.hpp:136
virtual std::string getFileName() const
Return the basename of this entry.
Definition: fileentry.cpp:320
std::string m_comment
Definition: fileentry.hpp:132
virtual void setTime(dostime_t time)
Set the FileEntry time using a DOS time.
Definition: fileentry.cpp:653
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 dostime_t getTime() const
Get the MS-DOS date/time of this entry.
Definition: fileentry.cpp:349
virtual void setComment(std::string const &comment)
Set the comment field for the FileEntry.
Definition: fileentry.cpp:461
buffer_t m_extra_field
Definition: fileentry.hpp:139
virtual size_t getCompressedSize() const
Retrive the size of the file when compressed.
Definition: fileentry.cpp:164
static CompressionLevel const COMPRESSION_LEVEL_MAXIMUM
Definition: fileentry.hpp:92
virtual std::string getComment() const
Retrieve the comment of the file entry.
Definition: fileentry.cpp:150
bool hasCrc() const
Check whether the CRC32 was defined.
Definition: fileentry.cpp:388
virtual std::string getName() const
Return the filename of the entry.
Definition: fileentry.cpp:303
virtual bool isValid() const
Check whether this entry is valid.
Definition: fileentry.cpp:447
virtual void setSize(size_t size)
Sets the size field for the entry.
Definition: fileentry.cpp:637
virtual void setUnixTime(std::time_t time)
Sets the time field in Unix time format for the entry.
Definition: fileentry.cpp:668
virtual void setExtra(buffer_t const &extra)
Set the extra field buffer.
Definition: fileentry.cpp:527
virtual void read(std::istream &is)
Read this FileEntry from the input stream.
Definition: fileentry.cpp:724
virtual void setLevel(CompressionLevel level)
Define the level of compression to use by this FileEntry.
Definition: fileentry.cpp:548
size_t m_uncompressed_size
Definition: fileentry.hpp:133
virtual bool isDirectory() const
Check whether the filename represents a directory.
Definition: fileentry.cpp:402
CompressionLevel m_compression_level
Definition: fileentry.hpp:137
std::streampos m_entry_offset
Definition: fileentry.hpp:135
static CompressionLevel const COMPRESSION_LEVEL_NONE
Definition: fileentry.hpp:90
virtual void setCrc(crc32_t crc)
Save the CRC of the entry.
Definition: fileentry.cpp:492
virtual StorageMethod getMethod() const
Return the method used to create this entry.
Definition: fileentry.cpp:282
virtual CompressionLevel getLevel() const
Retrieve the compression level.
Definition: fileentry.cpp:262
virtual std::string toString() const
Returns a human-readable string representation of the entry.
Definition: fileentry.cpp:683
virtual crc32_t getCrc() const
Return the CRC of the entry.
Definition: fileentry.cpp:182
virtual void setCompressedSize(size_t size)
Set the size when the file is compressed.
Definition: fileentry.cpp:480
virtual buffer_t getExtra() const
Some extra data to be stored along the entry.
Definition: fileentry.cpp:222
std::streampos getEntryOffset() const
Get the offset of this entry in a Zip archive.
Definition: fileentry.cpp:202
FilePath m_filename
Definition: fileentry.hpp:131
virtual std::time_t getUnixTime() const
Get the Unix date/time of this entry.
Definition: fileentry.cpp:374
virtual bool isEqual(FileEntry const &file_entry) const
Compare two file entries for equality.
Definition: fileentry.cpp:425
virtual void write(std::ostream &os)
Write this FileEntry to the output stream.
Definition: fileentry.cpp:742
virtual void setMethod(StorageMethod method)
Sets the storage method field for the entry.
Definition: fileentry.cpp:585
FileEntry(FilePath const &filename, std::string const &comment=std::string())
Initialize a FileEntry object.
Definition: fileentry.cpp:100
uint32_t crc32_t
Definition: fileentry.hpp:80
virtual size_t getHeaderSize() const
Retrieve the size of the header.
Definition: fileentry.cpp:237
virtual ~FileEntry()
Clean up a FileEntry object.
Definition: fileentry.cpp:134
void setEntryOffset(std::streampos offset)
Defines the position of the entry in a Zip archive.
Definition: fileentry.cpp:512
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:386
std::string filename() const
Retrieve the basename.
Definition: filepath.cpp:304
An IOException is used to signal an I/O error.
Exception used when it is not possible to move forward.
time_t dos2unixtime(dostime_t dostime)
Convert a DOS time to a Unix time.
Definition: dostime.c:131
dostime_t unix2dostime(time_t unix_time)
Convert a Unix date to a DOS date.
Definition: dostime.c:219
dostime_t dostime(int year, int month, int day, int hour, int minute, int second)
Definition: dostime.c:188
Definitions for the MS-DOS to Unix time conversions.
Define the zipios::FileEntry class.
The zipios namespace includes the Zipios++ library definitions.
Definition: backbuffer.cpp:36
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:48
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.