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 
32 #include "zipios/fileentry.hpp"
33 
35 
36 #include "zipios_common.hpp"
37 
38 
39 namespace zipios
40 {
41 
99 FileEntry::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 
134 {
135 }
136 
137 
149 std::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 
201 std::streampos FileEntry::getEntryOffset() const
202 {
203  return m_entry_offset;
204 }
205 
206 
222 {
223  return m_extra_field;
224 }
225 
226 
237 {
238  return 0;
239 }
240 
241 
262 {
263  if(isDirectory())
264  {
265  return COMPRESSION_LEVEL_NONE;
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
287  return StorageMethod::STORED;
288  }
289  return m_compress_method;
290 }
291 
292 
293 
294 
302 std::string FileEntry::getName() const
303 {
304  return m_filename;
305 }
306 
307 
319 std::string FileEntry::getFileName() const
320 {
321  return m_filename.filename();
322 }
323 
324 
331 size_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 
380 std::time_t FileEntry::getUnixTime() const
381 {
382  return m_unix_time;
383 }
384 
385 
394 bool FileEntry::hasCrc() const
395 {
396  return m_has_crc_32;
397 }
398 
399 
409 {
410  return m_filename.isDirectory();
411 }
412 
413 
431 bool 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 
453 bool FileEntry::isValid() const
454 {
455  return m_valid;
456 }
457 
458 
467 void 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 
518 void FileEntry::setEntryOffset(std::streampos offset)
519 {
520  m_entry_offset = offset;
521 }
522 
523 
533 void FileEntry::setExtra(buffer_t const& extra)
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 
643 void 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 
676 void FileEntry::setUnixTime(std::time_t time)
677 {
678  m_unix_time = time;
679 }
680 
681 
691 std::string FileEntry::toString() const
692 {
693  OutputStringStream sout;
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 
732 void 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 
750 void 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 
767 std::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
virtual std::time_t getUnixTime() const
Get the Unix date/time of this entry.
Definition: fileentry.cpp:380
std::time_t getUnixTimestamp() const
Retrieve the DOSDateTime as a Unix timestamp.
StorageMethod m_compress_method
Definition: fileentry.hpp:137
bool hasCrc() const
Check whether the CRC32 was defined.
Definition: fileentry.cpp:394
std::ostream & operator<<(std::ostream &os, FileCollection const &collection)
Write a FileCollection to the output stream.
The zipios namespace includes the Zipios library definitions.
Definition: backbuffer.cpp:35
virtual std::string getName() const
Return the filename of the entry.
Definition: fileentry.cpp:302
virtual void setUnixTime(std::time_t time)
Sets the time field in Unix time format for the entry.
Definition: fileentry.cpp:676
std::string filename() const
Retrieve the basename.
Definition: filepath.cpp:306
virtual void read(std::istream &is)
Read this FileEntry from the input stream.
Definition: fileentry.cpp:732
virtual buffer_t getExtra() const
Some extra data to be stored along the entry.
Definition: fileentry.cpp:221
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
virtual StorageMethod getMethod() const
Return the method used to create this entry.
Definition: fileentry.cpp:281
virtual bool isDirectory() const
Check whether the filename represents a directory.
Definition: fileentry.cpp:408
virtual void write(std::ostream &os)
Write this FileEntry to the output stream.
Definition: fileentry.cpp:750
dosdatetime_t getDOSDateTime() const
Retrieve the DOSDateTime value as is.
void setUnixTimestamp(std::time_t unix_timestamp)
Set the DOSDateTime value from a Unix timestamp.
virtual CompressionLevel getLevel() const
Retrieve the compression level.
Definition: fileentry.cpp:261
buffer_t m_extra_field
Definition: fileentry.hpp:140
virtual std::string getFileName() const
Return the basename of this entry.
Definition: fileentry.cpp:319
virtual size_t getSize() const
Retrieve the size of the file when uncompressed.
Definition: fileentry.cpp:331
CompressionLevel m_compression_level
Definition: fileentry.hpp:138
virtual DOSDateTime::dosdatetime_t getTime() const
Get the MS-DOS date/time of this entry.
Definition: fileentry.cpp:348
StorageMethod
The types used with FileEntry::setMethod and FileEntry::getMethod.
Definition: fileentry.hpp:48
virtual crc32_t getCrc() const
Return the CRC of the entry.
Definition: fileentry.cpp:181
virtual size_t getHeaderSize() const
Retrieve the size of the header.
Definition: fileentry.cpp:236
static CompressionLevel const COMPRESSION_LEVEL_NONE
Definition: fileentry.hpp:90
int CompressionLevel
The compression level to be used to save an entry.
Definition: fileentry.hpp:85
virtual void setComment(std::string const &comment)
Set the comment field for the FileEntry.
Definition: fileentry.cpp:467
FilePath m_filename
Definition: fileentry.hpp:132
virtual void setSize(size_t size)
Sets the size field for the entry.
Definition: fileentry.cpp:643
static CompressionLevel const COMPRESSION_LEVEL_MINIMUM
Definition: fileentry.hpp:91
void setEntryOffset(std::streampos offset)
Defines the position of the entry in a Zip archive.
Definition: fileentry.cpp:518
virtual bool isEqual(FileEntry const &file_entry) const
Compare two file entries for equality.
Definition: fileentry.cpp:431
bool isDirectory() const
Check whether the file is a directory.
Definition: filepath.cpp:388
virtual std::string toString() const
Returns a human-readable string representation of the entry.
Definition: fileentry.cpp:691
uint32_t crc32_t
Definition: fileentry.hpp:81
An IOException is used to signal an I/O error.
virtual void setMethod(StorageMethod method)
Sets the storage method field for the entry.
Definition: fileentry.cpp:591
virtual void setExtra(buffer_t const &extra)
Set the extra field buffer.
Definition: fileentry.cpp:533
virtual ~FileEntry()
Clean up a FileEntry object.
Definition: fileentry.cpp:133
A FileEntry represents an entry in a FileCollection.
Definition: fileentry.hpp:75
FileEntry(FilePath const &filename, std::string const &comment=std::string())
Initialize a FileEntry object.
Definition: fileentry.cpp:99
Exception used when it is not possible to move forward.
virtual std::string getComment() const
Retrieve the comment of the file entry.
Definition: fileentry.cpp:149
std::streampos getEntryOffset() const
Get the offset of this entry in a Zip archive.
Definition: fileentry.cpp:201
virtual void setCompressedSize(size_t size)
Set the size when the file is compressed.
Definition: fileentry.cpp:486
virtual size_t getCompressedSize() const
Retrieve the size of the file when compressed.
Definition: fileentry.cpp:163
Handle a file path and name and its statistics.
Definition: filepath.hpp:46
std::vector< unsigned char > buffer_t
Definition: fileentry.hpp:80
Various functions used throughout the library.
std::ostringstream OutputStringStream
An output stream using strings.
static CompressionLevel const COMPRESSION_LEVEL_MAXIMUM
Definition: fileentry.hpp:92
virtual void setTime(DOSDateTime::dosdatetime_t time)
Set the FileEntry time using a DOS time.
Definition: fileentry.cpp:659
virtual void setCrc(crc32_t crc)
Save the CRC of the entry.
Definition: fileentry.cpp:498
virtual bool isValid() const
Check whether this entry is valid.
Definition: fileentry.cpp:453
Define the zipios::FileEntry class.
void setDOSDateTime(dosdatetime_t datetime)
Set the DOSDateTime value as is.
size_t m_uncompressed_size
Definition: fileentry.hpp:134
std::streampos m_entry_offset
Definition: fileentry.hpp:136
std::string m_comment
Definition: fileentry.hpp:133
virtual void setLevel(CompressionLevel level)
Define the level of compression to use by this FileEntry.
Definition: fileentry.cpp:554