xref: /aosp_15_r20/external/lzma/CPP/7zip/Archive/Zip/ZipOut.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // ZipOut.h
2 
3 #ifndef ZIP7_INC_ZIP_OUT_H
4 #define ZIP7_INC_ZIP_OUT_H
5 
6 #include "../../../Common/MyCom.h"
7 
8 #include "../../Common/OutBuffer.h"
9 
10 #include "ZipItem.h"
11 
12 namespace NArchive {
13 namespace NZip {
14 
15 class CItemOut: public CItem
16 {
17 public:
18   FILETIME Ntfs_MTime;
19   FILETIME Ntfs_ATime;
20   FILETIME Ntfs_CTime;
21   bool Write_NtfsTime;
22   bool Write_UnixTime;
23 
24   // It's possible that NtfsTime is not defined, but there is NtfsTime in Extra.
25 
26   CByteBuffer Name_Utf; // for Info-Zip (kIzUnicodeName) Extra
27 
Get_UtfName_ExtraSize()28   size_t Get_UtfName_ExtraSize() const
29   {
30     const size_t size = Name_Utf.Size();
31     if (size == 0)
32       return 0;
33     return 4 + 5 + size;
34   }
35 
CItemOut()36   CItemOut():
37       Write_NtfsTime(false),
38       Write_UnixTime(false)
39       {}
40 };
41 
42 
43 // COutArchive can throw CSystemException and COutBufferException
44 
45 class COutArchive
46 {
47   COutBuffer m_OutBuffer;
48   CMyComPtr<IOutStream> m_Stream;
49 
50   UInt64 m_Base; // Base of archive (offset in output Stream)
51   UInt64 m_CurPos; // Curent position in archive (relative from m_Base)
52   UInt64 m_LocalHeaderPos; // LocalHeaderPos (relative from m_Base) for last WriteLocalHeader() call
53 
54   UInt32 m_LocalFileHeaderSize;
55   UInt32 m_ExtraSize;
56   bool m_IsZip64;
57 
58   void WriteBytes(const void *data, size_t size);
59   void Write8(Byte b);
60   void Write16(UInt16 val);
61   void Write32(UInt32 val);
62   void Write64(UInt64 val);
WriteNtfsTime(const FILETIME & ft)63   void WriteNtfsTime(const FILETIME &ft)
64   {
65     Write32(ft.dwLowDateTime);
66     Write32(ft.dwHighDateTime);
67   }
68 
69   void WriteTimeExtra(const CItemOut &item, bool writeNtfs);
70   void WriteUtfName(const CItemOut &item);
71   void WriteExtra(const CExtraBlock &extra);
72   void WriteCommonItemInfo(const CLocalItem &item, bool isZip64);
73   void WriteCentralHeader(const CItemOut &item);
74 
75   void SeekToCurPos();
76 public:
77   CMyComPtr<IStreamSetRestriction> SetRestriction;
78 
79   HRESULT ClearRestriction();
80   HRESULT SetRestrictionFromCurrent();
81   HRESULT Create(IOutStream *outStream);
82 
GetCurPos()83   UInt64 GetCurPos() const { return m_CurPos; }
84 
MoveCurPos(UInt64 distanceToMove)85   void MoveCurPos(UInt64 distanceToMove)
86   {
87     m_CurPos += distanceToMove;
88   }
89 
90   void WriteLocalHeader(CItemOut &item, bool needCheck = false);
91   void WriteLocalHeader_Replace(CItemOut &item);
92 
93   void WriteDescriptor(const CItemOut &item);
94 
95   HRESULT WriteCentralDir(const CObjectVector<CItemOut> &items, const CByteBuffer *comment);
96 
97   void CreateStreamForCompressing(CMyComPtr<IOutStream> &outStream);
98   void CreateStreamForCopying(CMyComPtr<ISequentialOutStream> &outStream);
99 };
100 
101 }}
102 
103 #endif
104