• C/C++操作加密与不加密的zip文件


        为了后续的方便操作zip文件, 将所有的操作封装成了一个动态库了。

    1. /**
    2. * \description 从压缩包文件中解压出指定的文件到指定的目录.
    3. * \author sunsz
    4. * \date 2023/09/09
    5. *
    6. */
    7. LIBZIP_API int UnpackFile(const char* password, char zipfilename[], char filename_to_extract[], char filepath_to_extract[]);
    8. /**
    9. * \description .从压缩包文件中读出指定的文件到buffer中.
    10. *
    11. * \author sunsz
    12. * \date 2023/09/09
    13. *
    14. */
    15. LIBZIP_API int UnpackBuffer(const char* password, char zipfilename[],char filenameinzip[], char *buffer,unsigned int *buffer_size);
    16. /**
    17. * \description 获取指定文件的大小.
    18. *
    19. * \author sunsz
    20. * \date 2023/09/09
    21. *
    22. */
    23. LIBZIP_API int UnpackBufferSize(const char* password, char zipfilename[], char filenameinzip[]);
    24. /**
    25. * \description 从压缩包中获取文件列表.
    26. *
    27. * \author sunsz
    28. * \date 2023/09/09
    29. *
    30. */
    31. LIBZIP_API int UnpackFileList(const char* password, char zipfilename[], char filename_list[256][256]);
    32. /**
    33. * \description 将指定的文件写入到压缩包文件中.
    34. * opt_overwrite = 0 重新新建文件 opt_overwrite = 2 以追加的方式添加到压缩包不存在则创建
    35. * \author sunsz
    36. * \date 2023/09/09
    37. *
    38. */
    39. LIBZIP_API int PackFile(const char* password,char m_zipname[],char filein[],int opt_overwrite);
    40. /**
    41. * \description 将内容以指定的文件名称存入到压缩包文件中.
    42. * opt_overwrite = 0 重新新建文件 opt_overwrite = 2 以追加的方式添加到压缩包不存在则创建
    43. * \author sunsz
    44. * \date 2023/09/09
    45. *
    46. */
    47. LIBZIP_API int PackBuffer(const char* password, char m_zipname[], char filenameinzip[], char* buffer, unsigned int buffer_size, int opt_overwrite);

     示例代码:

    1. //依赖头文件
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. //定义函数指针
    9. //typedef int(WINAPI*S_dll_callback_demo1)(unsigned char*,unsigned char*);
    10. typedef int(WINAPI*ZipRead)(const char*, char*, char*, char*);
    11. typedef int(WINAPI*ZipReadFileList)(const char*, char*, char**);
    12. typedef int(WINAPI*ZipWrite)(const char*, char*, char*,int);
    13. typedef int(WINAPI* ZipWriteBuffter)(const char* , char *, char*, char* , unsigned int , int );
    14. typedef int(WINAPI* ZipReadBuffer)(const char* , char *, char *, char *, unsigned int* );
    15. typedef int(WINAPI* ZipReadBufferSize)(const char* , char *, char *);
    16. //定义API函数变量
    17. static ZipWrite zipWriteFile;
    18. static ZipRead zipReadFile;
    19. static ZipReadFileList readList;
    20. static ZipWriteBuffter zipWriteBuffter;
    21. static ZipReadBuffer zipReadBuffter;
    22. static ZipReadBufferSize zipReadBuffterSize;
    23. int main(){
    24. int i = 0;
    25. int j = 0;
    26. int malloclen = 0;
    27. unsigned int len = 0;
    28. char* dataout = NULL;
    29. char* passwd = "123456";
    30. int filenum = 0;
    31. char filelist[256][256] = { 0x00 };
    32. char data[] = "hello zip!";
    33. char path[] = ".//libzip.dll";
    34. HMODULE hDll = LoadLibrary(path);
    35. if (hDll != NULL) {
    36. zipWriteFile = (ZipWrite)GetProcAddress(hDll, "PackFile");
    37. zipReadFile = (ZipRead)GetProcAddress(hDll, "UnpackFile");
    38. readList = (ZipReadFileList)GetProcAddress(hDll, "UnpackFileList");
    39. zipWriteBuffter = (ZipWriteBuffter)GetProcAddress(hDll, "PackBuffer");
    40. zipReadBuffter = (ZipReadBuffer)GetProcAddress(hDll, "UnpackBuffer");
    41. zipReadBuffterSize = (ZipReadBufferSize)GetProcAddress(hDll, "UnpackBufferSize");
    42. /*if (zipWriteFile) {
    43. zipWriteFile(passwd, "2.zip", "D:\\D-Pro\\Test\\test_zip\\test_zip\\test.S19",0);
    44. }
    45. else {
    46. printf("\nZipWriteFile\n");
    47. }
    48. if (zipReadFile) {
    49. zipReadFile(passwd, "2.zip", "test.S19",".//test//11111.s19");
    50. }
    51. else {
    52. printf("\nZipReadFile\n");
    53. }*/
    54. if (zipWriteBuffter) {
    55. zipWriteBuffter(passwd, "3.zip", "11112.txt", data, sizeof(data), 2);
    56. }
    57. else {
    58. printf("\nzipWriteBuffter \n");
    59. }
    60. if (zipReadBuffter) {
    61. if (zipReadBuffterSize) {
    62. malloclen = zipReadBuffterSize(passwd, "3.zip", "11112.txt");
    63. if (malloclen) {
    64. dataout = (char*)malloc(malloclen + 8);
    65. zipReadBuffter(passwd, "3.zip", "11112.txt", dataout, &len);
    66. printf("%s\n", dataout);
    67. if (dataout != NULL) {
    68. free(dataout);
    69. dataout = NULL;
    70. }
    71. }
    72. }
    73. }
    74. else {
    75. printf("\nzipWriteBuffter \n");
    76. }
    77. }
    78. else
    79. {
    80. printf("\n加载失败!\n");
    81. }
    82. printf("\n请按任意键结束!\n");
    83. _getch();
    84. return 0;
    85. }

    示例测试: 

  • 相关阅读:
    软件设计师2010上午题基础知识(易错整理)
    数据库课件= =
    自行开发一个安卓相册的功能说明
    某微e-office协同管理系统存在任意文件读取漏洞分析 CNVD-2022-07603
    vue3利用 a 标签,文件流,JSZip 压缩包,实现文件下载
    【日常记录】CTF审查清单(windows)
    Linux之shell脚本编程、多命令、脚本、bc计算器、反引号、if语句、for语句
    景区门票管理系统
    南山区民政局关于开展2022年度南山区社会组织等级评估工作的通知
    MySQL connection-control限制登录次数插件安装
  • 原文地址:https://blog.csdn.net/github_37687123/article/details/132780562