闲来无事随便写下,感兴趣的可以参考这里:

http://www.pkware.com/documents/casestudies/APPNOTE.TXT

#4.3.7 Local file header:的部分

更详细的可参考非常nb的zlib:

http://www.zlib.net/

 

主要的作用是读取一个zip文件,列出压缩前的文件列表。

比如:

y@y-VirtualBox:~/MyDocuments/Training$ ./a.out test.zip
page0.bmp
page1.bmp
page2.bmp
page3.bmp

代码:

Code Snippet
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char *argv[])
  4. {
  5.     if (argc != 2)
  6.     {
  7.         printf("usage: ./a.out xxx.zip.\n");
  8.         return -1;
  9.     }
  10.  
  11.     FILE* fp = fopen(argv[1], "r");
  12.  
  13.     if (!fp)
  14.     {
  15.         printf("file %s not found.\n");
  16.         return -1;
  17.     }
  18.  
  19.     fseek(fp, 0, SEEK_END);
  20.     int fileSize = ftell(fp);
  21.     fseek(fp, 0, SEEK_SET);
  22.     char* fileContent = (char*)malloc(fileSize * sizeof(char));
  23.     if (fileSize != fread(fileContent, 1, fileSize, fp))
  24.     {
  25.         printf("read error!\n");
  26.     }
  27.     
  28.     int index = 0;
  29.     int centralFileHeaderSignature = 0x02014b50;
  30.     while (index < fileSize)
  31.     {
  32.         if (*(int*)(fileContent + index) == centralFileHeaderSignature)
  33.         {
  34.             unsigned short fileNameLength = *(unsigned short*)(fileContent + index + 28);
  35.             char* fileName = (char*)malloc((fileNameLength+1) * sizeof(char));
  36.             strncpy(fileName, fileContent + index + 46, fileNameLength);
  37.             printf("%s\n", fileName);
  38.             free(fileName);
  39.         }
  40.         index++;
  41.     }
  42.  
  43.     free(fileContent);
  44.     fclose(fp);
  45.  
  46.     return 0;
  47. }

有两个疑惑的地方:

1.对于所谓的signature0x01024b50,如何做到压缩后的内容肯定不会是这个值呢,这个估计要看下压缩算法才明白。

2.查找signature的方法太朴素了,应当如何改进?

3.如何在不一次读取整个文件内容得到结果?