19 #include "minizip/export.h" 20 #include "minizip/unzip.h" 22 #define READ_8(adr) ((unsigned char)*(adr)) 23 #define READ_16(adr) ( READ_8(adr) | (READ_8(adr+1) << 8) ) 24 #define READ_32(adr) ( READ_16(adr) | (READ_16((adr)+2) << 16) ) 26 #define WRITE_8(buff, n) do { \ 27 *((unsigned char*)(buff)) = (unsigned char) ((n) & 0xff); \ 29 #define WRITE_16(buff, n) do { \ 30 WRITE_8((unsigned char*)(buff), n); \ 31 WRITE_8(((unsigned char*)(buff)) + 1, (n) >> 8); \ 33 #define WRITE_32(buff, n) do { \ 34 WRITE_16((unsigned char*)(buff), (n) & 0xffff); \ 35 WRITE_16((unsigned char*)(buff) + 2, (n) >> 16); \ 38 extern int MINIZIP_API unzRepair(file, fileOut, fileOutTmp, nRecovered, bytesRecovered)
41 const
char* fileOutTmp;
43 uLong* bytesRecovered;
46 FILE* fpZip = fopen(file,
"rb");
47 FILE* fpOut = fopen(fileOut,
"wb");
48 FILE* fpOutCD = fopen(fileOutTmp,
"wb");
49 if (fpZip != NULL && fpOut != NULL)
58 while ( fread(header, 1, 30, fpZip) == 30 )
60 int currentOffset = offset;
63 if (READ_32(header) == 0x04034b50)
65 unsigned int version = READ_16(header + 4);
66 unsigned int gpflag = READ_16(header + 6);
67 unsigned int method = READ_16(header + 8);
68 unsigned int filetime = READ_16(header + 10);
69 unsigned int filedate = READ_16(header + 12);
70 unsigned int crc = READ_32(header + 14);
71 unsigned int cpsize = READ_32(header + 18);
72 unsigned int uncpsize = READ_32(header + 22);
73 unsigned int fnsize = READ_16(header + 26);
74 unsigned int extsize = READ_16(header + 28);
75 filename[0] = extra[0] =
'\0';
78 if (fwrite(header, 1, 30, fpOut) == 30)
91 if (fread(filename, 1, fnsize, fpZip) == fnsize)
93 if (fwrite(filename, 1, fnsize, fpOut) == fnsize)
111 err = Z_STREAM_ERROR;
118 if (fread(extra, 1, extsize, fpZip) == extsize)
120 if (fwrite(extra, 1, extsize, fpOut) == extsize)
139 int dataSize = cpsize;
146 char* data = malloc(dataSize);
149 if ((
int)fread(data, 1, dataSize, fpZip) == dataSize)
151 if ((
int)fwrite(data, 1, dataSize, fpOut) == dataSize)
154 totalBytes += dataSize;
183 int comsize = (int) strlen(comment);
184 WRITE_32(header, 0x02014b50);
185 WRITE_16(header + 4, version);
186 WRITE_16(header + 6, version);
187 WRITE_16(header + 8, gpflag);
188 WRITE_16(header + 10, method);
189 WRITE_16(header + 12, filetime);
190 WRITE_16(header + 14, filedate);
191 WRITE_32(header + 16, crc);
192 WRITE_32(header + 20, cpsize);
193 WRITE_32(header + 24, uncpsize);
194 WRITE_16(header + 28, fnsize);
195 WRITE_16(header + 30, extsize);
196 WRITE_16(header + 32, comsize);
197 WRITE_16(header + 34, 0);
198 WRITE_16(header + 36, 0);
199 WRITE_32(header + 38, 0);
200 WRITE_32(header + 42, currentOffset);
202 if (fwrite(header, 1, 46, fpOutCD) == 46)
209 if (fwrite(filename, 1, fnsize, fpOutCD) == fnsize)
221 err = Z_STREAM_ERROR;
228 if (fwrite(extra, 1, extsize, fpOutCD) == extsize)
242 if ((
int)fwrite(comment, 1, comsize, fpOutCD) == comsize)
274 int entriesZip = entries;
277 int comsize = (int) strlen(comment);
278 if (entriesZip > 0xffff)
282 WRITE_32(header, 0x06054b50);
283 WRITE_16(header + 4, 0);
284 WRITE_16(header + 6, 0);
285 WRITE_16(header + 8, entriesZip);
286 WRITE_16(header + 10, entriesZip);
287 WRITE_32(header + 12, offsetCD);
288 WRITE_32(header + 16, offset);
289 WRITE_16(header + 20, comsize);
292 if (fwrite(header, 1, 22, fpOutCD) == 22)
298 if ((
int)fwrite(comment, 1, comsize, fpOutCD) != comsize)
315 fpOutCD = fopen(fileOutTmp,
"rb");
320 while ( (nRead = (
int)fread(buffer, 1,
sizeof(buffer), fpOutCD)) > 0)
322 if ((
int)fwrite(buffer, 1, nRead, fpOut) != nRead)
337 (void)
remove(fileOutTmp);
342 if (nRecovered != NULL)
344 *nRecovered = entries;
346 if (bytesRecovered != NULL)
348 *bytesRecovered = totalBytes;
354 err = Z_STREAM_ERROR;