zipios  2.3.2
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-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 
39 namespace zipios
40 {
41 
99 FileEntry::FileEntry(FilePath const & filename, std::string const & comment)
100  : m_filename(filename)
101  , m_comment(comment)
102 {
103 }
104 
105 
126 {
127 }
128 
129 
141 std::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 
193 std::streampos FileEntry::getEntryOffset() const
194 {
195  return m_entry_offset;
196 }
197 
198 
214 {
215  return m_extra_field;
216 }
217 
218 
229 {
230  return 0;
231 }
232 
233 
254 {
255  if(isDirectory())
256  {
257  return COMPRESSION_LEVEL_NONE;
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
279  return StorageMethod::STORED;
280  }
281  return m_compress_method;
282 }
283 
284 
285 
286 
294 std::string FileEntry::getName() const
295 {
296  return m_filename;
297 }
298 
299 
311 std::string FileEntry::getFileName() const
312 {
313  return m_filename.filename();
314 }
315 
316 
323 std::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 
376 std::time_t FileEntry::getUnixTime() const
377 {
378  return m_unix_time;
379 }
380 
381 
390 bool FileEntry::hasCrc() const
391 {
392  return m_has_crc_32;
393 }
394 
395 
405 {
406  return m_filename.isDirectory();
407 }
408 
409 
430 bool 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 
452 bool FileEntry::isValid() const
453 {
454  return m_valid;
455 }
456 
457 
466 void 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 
517 void FileEntry::setEntryOffset(std::streampos offset)
518 {
519  m_entry_offset = offset;
520 }
521 
522 
532 void 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 
642 void 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 
675 void FileEntry::setUnixTime(std::time_t time)
676 {
677  m_unix_time = time;
678 }
679 
680 
690 std::string FileEntry::toString() const
691 {
692  OutputStringStream sout;
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 
731 void 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 
749 void 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 
768 std::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
StorageMethod m_compress_method
Definition: fileentry.hpp:137
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 void setUnixTime(std::time_t time)
Sets the time field in Unix time format for the entry.
Definition: fileentry.cpp:675
virtual void read(std::istream &is)
Read this FileEntry from the input stream.
Definition: fileentry.cpp:731
virtual std::size_t getSize() const
Retrieve the size of the file when uncompressed.
Definition: fileentry.cpp:323
dosdatetime_t getDOSDateTime() const
Retrieve the DOSDateTime value as is.
Various exceptions used throughout the Zipios library, all based on zipios::Exception.
virtual std::string getFileName() const
Return the basename of this entry.
Definition: fileentry.cpp:311
std::string filename() const
Retrieve the basename.
Definition: filepath.cpp:315
virtual void write(std::ostream &os)
Write this FileEntry to the output stream.
Definition: fileentry.cpp:749
std::time_t getUnixTimestamp() const
Retrieve the DOSDateTime as a Unix timestamp.
void setUnixTimestamp(std::time_t unix_timestamp)
Set the DOSDateTime value from a Unix timestamp.
buffer_t m_extra_field
Definition: fileentry.hpp:140
virtual std::string getComment() const
Retrieve the comment of the file entry.
Definition: fileentry.cpp:141
virtual std::size_t getHeaderSize() const
Retrieve the size of the header.
Definition: fileentry.cpp:228
CompressionLevel m_compression_level
Definition: fileentry.hpp:138
virtual StorageMethod getMethod() const
Return the method used to create this entry.
Definition: fileentry.cpp:273
StorageMethod
The types used with FileEntry::setMethod and FileEntry::getMethod.
Definition: fileentry.hpp:48
virtual bool isEqual(FileEntry const &file_entry) const
Compare two file entries for equality.
Definition: fileentry.cpp:430
static CompressionLevel const COMPRESSION_LEVEL_NONE
Definition: fileentry.hpp:90
bool hasCrc() const
Check whether the CRC32 was defined.
Definition: fileentry.cpp:390
int CompressionLevel
The compression level to be used to save an entry.
Definition: fileentry.hpp:85
virtual std::time_t getUnixTime() const
Get the Unix date/time of this entry.
Definition: fileentry.cpp:376
virtual void setComment(std::string const &comment)
Set the comment field for the FileEntry.
Definition: fileentry.cpp:466
virtual crc32_t getCrc() const
Return the CRC of the entry.
Definition: fileentry.cpp:173
FilePath m_filename
Definition: fileentry.hpp:132
virtual void setSize(size_t size)
Sets the size field for the entry.
Definition: fileentry.cpp:642
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:517
virtual std::string toString() const
Returns a human-readable string representation of the entry.
Definition: fileentry.cpp:690
virtual bool isValid() const
Check whether this entry is valid.
Definition: fileentry.cpp:452
bool isDirectory() const
Check whether the file is a directory.
Definition: filepath.cpp:422
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:590
virtual std::size_t getCompressedSize() const
Retrieve the size of the file when compressed.
Definition: fileentry.cpp:155
virtual void setExtra(buffer_t const &extra)
Set the extra field buffer.
Definition: fileentry.cpp:532
virtual ~FileEntry()
Clean up a FileEntry object.
Definition: fileentry.cpp:125
virtual std::string getName() const
Return the filename of the entry.
Definition: fileentry.cpp:294
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 CompressionLevel getLevel() const
Retrieve the compression level.
Definition: fileentry.cpp:253
virtual bool isDirectory() const
Check whether the filename represents a directory.
Definition: fileentry.cpp:404
virtual void setCompressedSize(size_t size)
Set the size when the file is compressed.
Definition: fileentry.cpp:485
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 buffer_t getExtra() const
Some extra data to be stored along the entry.
Definition: fileentry.cpp:213
virtual void setTime(DOSDateTime::dosdatetime_t time)
Set the FileEntry time using a DOS time.
Definition: fileentry.cpp:658
std::streampos getEntryOffset() const
Get the offset of this entry in a Zip archive.
Definition: fileentry.cpp:193
virtual void setCrc(crc32_t crc)
Save the CRC of the entry.
Definition: fileentry.cpp:497
Define the zipios::FileEntry class.
void setDOSDateTime(dosdatetime_t datetime)
Set the DOSDateTime value as is.
std::streampos m_entry_offset
Definition: fileentry.hpp:136
virtual DOSDateTime::dosdatetime_t getTime() const
Get the MS-DOS date/time of this entry.
Definition: fileentry.cpp:344
std::string m_comment
Definition: fileentry.hpp:133
std::size_t m_uncompressed_size
Definition: fileentry.hpp:134
virtual void setLevel(CompressionLevel level)
Define the level of compression to use by this FileEntry.
Definition: fileentry.cpp:553