Repository navigation
Expand file tree
/
Copy pathminizip.c
More file actions
178 lines (150 loc) · 4.43 KB
/
Copy pathminizip.c
File metadata and controls
178 lines (150 loc) · 4.43 KB
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
178
/* minizip.c
Version 1.1, February 14h, 2010
sample part of the MiniZip project
Copyright (C) 1998-2010 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
Modifications of Unzip for Zip64
Copyright (C) 2007-2008 Even Rouault
Modifications for Zip64 support
Copyright (C) 2009-2010 Mathias Svensson
http://result42.com
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#if (!defined(_WIN32)) && (!defined(WIN32)) && (!defined(__APPLE__))
# ifndef __USE_FILE_OFFSET64
# define __USE_FILE_OFFSET64
# endif
# ifndef __USE_LARGEFILE64
# define __USE_LARGEFILE64
# endif
# ifndef _LARGEFILE64_SOURCE
# define _LARGEFILE64_SOURCE
# endif
# ifndef _FILE_OFFSET_BIT
# define _FILE_OFFSET_BIT 64
# endif
#endif
#ifdef __APPLE__
/* In darwin and perhaps other BSD variants off_t is a 64 bit value, hence no need for specific 64 bit functions */
# define FOPEN_FUNC(filename, mode) fopen(filename, mode)
# define FTELLO_FUNC(stream) ftello(stream)
# define FSEEKO_FUNC(stream, offset, origin) fseeko(stream, offset, origin)
#else
# define FOPEN_FUNC(filename, mode) fopen64(filename, mode)
# define FTELLO_FUNC(stream) ftello64(stream)
# define FSEEKO_FUNC(stream, offset, origin) fseeko64(stream, offset, origin)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#ifdef _WIN32
# include <direct.h>
# include <io.h>
#else
# include <unistd.h>
# include <utime.h>
# include <sys/types.h>
# include <sys/stat.h>
#endif
#include "zip.h"
#ifdef _WIN32
# define USEWIN32IOAPI
# include "iowin32.h"
#endif
#define WRITEBUFFERSIZE (16384)
#define MAXFILENAME (256)
uLong filetime(const char *filename, tm_zip *tmzip, uLong *dostime)
{
int ret = 0;
#ifdef _WIN32
FILETIME ftLocal;
HANDLE hFind;
WIN32_FIND_DATAA ff32;
hFind = FindFirstFileA(filename, &ff32);
if (hFind != INVALID_HANDLE_VALUE)
{
FileTimeToLocalFileTime(&(ff32.ftLastWriteTime), &ftLocal);
FileTimeToDosDateTime(&ftLocal,((LPWORD)dostime)+1,((LPWORD)dostime)+0);
FindClose(hFind);
ret = 1;
}
#else
#if defined unix || defined __APPLE__
struct stat s = {0};
struct tm* filedate;
time_t tm_t = 0;
if (strcmp(filename,"-") != 0)
{
char name[MAXFILENAME+1];
int len = strlen(filename);
if (len > MAXFILENAME)
len = MAXFILENAME;
strncpy(name, filename, MAXFILENAME - 1);
name[MAXFILENAME] = 0;
if (name[len - 1] == '/')
name[len - 1] = 0;
/* not all systems allow stat'ing a file with / appended */
if (stat(name,&s) == 0)
{
tm_t = s.st_mtime;
ret = 1;
}
}
filedate = localtime(&tm_t);
tmzip->tm_sec = filedate->tm_sec;
tmzip->tm_min = filedate->tm_min;
tmzip->tm_hour = filedate->tm_hour;
tmzip->tm_mday = filedate->tm_mday;
tmzip->tm_mon = filedate->tm_mon ;
tmzip->tm_year = filedate->tm_year;
#endif
#endif
return ret;
}
int is_large_file(const char* filename)
{
ZPOS64_T pos = 0;
FILE* pFile = FOPEN_FUNC(filename, "rb");
if (pFile == NULL)
return 0;
FSEEKO_FUNC(pFile, 0, SEEK_END);
pos = FTELLO_FUNC(pFile);
fclose(pFile);
printf("File : %s is %lld bytes\n", filename, pos);
return (pos >= 0xffffffff);
}
/* Calculate the CRC32 of a file, because to encrypt a file, we need known the CRC32 of the file before */
int get_file_crc(const char* filenameinzip, void *buf, unsigned long size_buf, unsigned long* result_crc)
{
FILE *fin = NULL;
unsigned long calculate_crc = 0;
unsigned long size_read = 0;
int err = ZIP_OK;
fin = FOPEN_FUNC(filenameinzip,"rb");
if (fin == NULL)
err = ZIP_ERRNO;
else
{
do
{
size_read = (int)fread(buf,1,size_buf,fin);
if ((size_read < size_buf) && (feof(fin) == 0))
{
printf("error in reading %s\n",filenameinzip);
err = ZIP_ERRNO;
}
if (size_read > 0)
calculate_crc = crc32(calculate_crc,buf,size_read);
}
while ((err == ZIP_OK) && (size_read > 0));
}
if (fin)
fclose(fin);
printf("file %s crc %lx\n", filenameinzip, calculate_crc);
*result_crc = calculate_crc;
return err;
}