-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZipArchive.h
177 lines (158 loc) · 5 KB
/
ZipArchive.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Part of The TCMP Matroska CDL, and Matroska Shell Extension
*
* ZipArchive.h
*
* Copyright (C) Jory Stone - 2003
*
* The TCMP Matroska CDL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* The TCMP Matroska CDL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with The TCMP Matroska CDL; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*!
\file ZipArchive.h
\version \$Id: ZipArchive.h,v 1.3 2003/10/08 03:30:42 jcsston Exp $
\brief A lightwight Zip Archive class
\author Jory Stone <jcsston @ toughguy.net>
*/
#ifndef _ZIP_ARCHIVE_H_
#define _ZIP_ARCHIVE_H_
#include <windows.h>
#include <stdio.h>
#include <vector>
#include <string>
#include <string.h>
#include <time.h>
#include "zlib.h"
#pragma pack(push, 1)
struct zip_local_file_header
{
DWORD header_sig;// 4bytes (0x04034b50)
WORD version;// needed to extract 2 bytes
WORD general_flags;// purpose bit flag 2 bytes
WORD compression_method;// 2 bytes
WORD last_mod_file_time;// 2 bytes
WORD last_mod_file_date;// 2 bytes
DWORD crc32;//-32 4 bytes
DWORD compressed_size; // 4 bytes
DWORD uncompressed_size; // 4 bytes
WORD file_name_length; // 2 bytes
WORD extra_field_length; // 2 bytes
//char *file_name; // (variable size)
//char *extra_field; // (variable size)
};
// Only used if bit 3 is set on zip_local_file_header::general_flags
struct zip_data_footer
{
DWORD crc32;//-32 4 bytes
DWORD compressed_size; // 4 bytes
DWORD uncompressed_size; // 4 bytes
};
struct zip_central_dir_header
{
DWORD header_sig; // 4 bytes (0x02014b50)
WORD version_by; // 2 bytes
WORD version_needed; // to extract 2 bytes
WORD general_flags;// purpose bit flag 2 bytes
WORD compression_method;// 2 bytes
WORD last_mod_file_time;// 2 bytes
WORD last_mod_file_date;// 2 bytes
DWORD crc32;//-32 4 bytes
DWORD compressed_size; // 4 bytes
DWORD uncompressed_size; // 4 bytes
WORD file_name_length; // 2 bytes
WORD extra_field_length; // 2 bytes
WORD comment_length; // 2 bytes
WORD disk_number_start; // 2 bytes
WORD internal_file_attributes; // 2 bytes
DWORD external_file_attributes; // 4 bytes
DWORD relative_offset_of_local_header; // 4 bytes
//char *file_name; // (variable size)
//char *extra_field; // (variable size)
//char *file_comment; // (variable size)
};
/*Digital signature:
header signature 4 bytes (0x05054b50)
size of data 2 bytes
signature data (variable size)*/
struct zip_central_dir_footer
{
DWORD header_sig; // 4 bytes (0x06054b50)
WORD disk_number; //number of this disk 2 bytes
WORD disk_number_dir; // 2 bytes
WORD entry_count_disk_total; //WORD total number of entries in the central directory on this disk 2 bytes
WORD entry_count_total; //; the central directory 2 bytes
DWORD size_central_dir;// 4 bytes
DWORD dir_offset; //ectory_with_respect_to_the_starting disk number;// 4 bytes
WORD file_comment_length;// 2 bytes
//char *file_comment;// (variable size)
};
#pragma pack(pop)
class CZip_local_file_header {
public:
CZip_local_file_header() {
memset(&data, 0, sizeof(zip_local_file_header));
data.header_sig = 0x04034b50;
};
zip_local_file_header data;
std::string file_name;
std::string extra_field;
};
class CZip_central_dir_header {
public:
CZip_central_dir_header() {
memset(&data, 0, sizeof(zip_central_dir_header));
data.header_sig = 0x02014b50;
data.version_by = 20;
};
void Write(FILE *targetFile) {
fwrite(&data, 1, sizeof(zip_central_dir_header), targetFile);
fwrite(file_name.c_str(), 1, file_name.length(), targetFile);
fwrite(extra_field.c_str(), 1, extra_field.length(), targetFile);
fwrite(file_comment.c_str(), 1, file_comment.length(), targetFile);
}
zip_central_dir_header data;
std::string file_name;
std::string extra_field;
std::string file_comment;
};
class ZipArchiveWriter {
public:
ZipArchiveWriter(const wchar_t *outputArchive);
ZipArchiveWriter(const std::string &outputArchive);
~ZipArchiveWriter();
bool IsOk();
void SetComment(const std::string &newComment) { m_ZipComment = newComment; };
void AddFile(const wchar_t *sourceFilename);
void AddFile(const std::string &sourceFilename);
void AddFile(const std::string &newFile, const unsigned char *data, unsigned long size);
void Close();
protected:
bool m_bClosed;
FILE *m_OutputFile;
std::string m_OutputFilename;
std::string m_ZipComment;
struct DosDate {
WORD nMonthDay : 5,
nMonth : 4,
nYear : 7;
};
struct DosTime {
WORD nSecond : 5,
nMinute : 6,
nHour : 5;
};
std::vector<CZip_central_dir_header> m_FileList;
};
#endif // _ZIP_ARCHIVE_H_