minizip c++ 压缩文件及文件夹

简介:
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
#include <fcntl.h>
#include <sys/stat.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
 
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
 
#include "minizip/zip.h"
#include "minizip/unzip.h"
 
using  namespace  std;
 
void  EnumDirFiles( const  string& dirPrefix, const  string& dirName,vector<string>& vFiles)
{
     if  (dirPrefix.empty() || dirName.empty())
         return ;
     string dirNameTmp = dirName;
     string dirPre = dirPrefix;
     
     if  (dirNameTmp.find_last_of( "/" ) != dirNameTmp.length() - 1)
         dirNameTmp +=  "/" ;
     if  (dirNameTmp[0] ==  '/' )
         dirNameTmp = dirNameTmp.substr(1);
     if  (dirPre.find_last_of( "/" ) != dirPre.length() - 1)
         dirPre +=  "/" ;
     
     string path;
 
     path = dirPre + dirNameTmp;
 
 
     struct  stat fileStat;
     DIR* pDir = opendir(path.c_str());
     if  (!pDir)  return ;
 
     struct  dirent* pDirEnt = NULL;
     while  ( (pDirEnt = readdir(pDir)) != NULL )
     {
         if  ( strcmp (pDirEnt->d_name, "." ) == 0 ||  strcmp (pDirEnt->d_name, ".." ) == 0)
             continue ;
 
         string tmpDir = dirPre + dirNameTmp + pDirEnt->d_name;
         if  (stat(tmpDir.c_str(),&fileStat) != 0)
             continue ;
 
         string innerDir = dirNameTmp + pDirEnt->d_name;
         if  (fileStat.st_mode & S_IFDIR == S_IFDIR)
         {
             EnumDirFiles(dirPrefix,innerDir,vFiles);
             continue ;
         }
 
         vFiles.push_back(innerDir);
     }
 
     if  (pDir)
         closedir(pDir);
}
 
int  writeInZipFile(zipFile zFile, const  string& file)
{
     fstream f(file.c_str(),std::ios::binary | std::ios::in);
     f.seekg(0, std::ios::end);
     long  size = f.tellg();
     f.seekg(0, std::ios::beg);
     if  ( size <= 0 )
     {
         return  zipWriteInFileInZip(zFile,NULL,0);
     }
     char * buf =  new  char [size];
     f.read(buf,size);
     int  ret = zipWriteInFileInZip(zFile,buf,size);
     delete [] buf;
     return  ret;
}
 
int  main( int  argc,  char  *argv[])
{
     if  (argc < 3)
     {
         cout<< "usage: mini from to" <<endl;
         return  -1;
     }
     string dest = string(argv[1]);
     string src = string(argv[2]);
     if  (src.find_last_of( "/" ) == src.length() - 1)
         src = src.substr(0,src.length()-1);
 
     struct  stat fileInfo;
     stat(src.c_str(), &fileInfo);
     if  (S_ISREG(fileInfo.st_mode))
     {
         zipFile zFile = zipOpen(dest.c_str(),APPEND_STATUS_CREATE);
         if  (zFile == NULL)
         {
             cout<< "openfile failed" <<endl;
             return  -1;
         }
         zip_fileinfo zFileInfo = { 0 };
         int  ret = zipOpenNewFileInZip(zFile,src.c_str(),&zFileInfo,NULL,0,NULL,0,NULL,0,Z_DEFAULT_COMPRESSION);
         if  (ret != ZIP_OK)
         {
             cout<< "openfile in zip failed" <<endl;
             zipClose(zFile,NULL);
             return  -1;
         }
         ret = writeInZipFile(zFile,src);
         if  (ret != ZIP_OK)
         {
             cout<< "write in zip failed" <<endl;
             zipClose(zFile,NULL);
             return  -1;
         }
         zipClose(zFile,NULL);
         cout<< "zip ok" <<endl;
     }
     else  if  (S_ISDIR(fileInfo.st_mode))
     {
         size_t  pos = src.find_last_of( "/" );
         string dirName = src.substr(pos + 1);
         string dirPrefix = src.substr(0,pos);
 
         zipFile zFile = zipOpen(dest.c_str(),APPEND_STATUS_CREATE);
         if  (zFile == NULL)
         {
             cout<< "openfile failed" <<endl;
             return  -1;
         }
 
         vector<string> vFiles;
         EnumDirFiles(dirPrefix,dirName,vFiles);
         vector<string>::iterator itF = vFiles.begin();
         for (;itF != vFiles.end(); ++itF)
         {
             zip_fileinfo zFileInfo = { 0 };
             int  ret = zipOpenNewFileInZip(zFile,itF->c_str(),&zFileInfo,NULL,0,NULL,0,NULL,0,Z_DEFAULT_COMPRESSION);
             if  (ret != ZIP_OK)
             {
                 cout<< "openfile in zip failed" <<endl;
                 zipClose(zFile,NULL);
                 return  -1;
             }
             ret = writeInZipFile(zFile,*itF);
             if  (ret != ZIP_OK)
             {
                 cout<< "write in zip failed" <<endl;
                 zipClose(zFile,NULL);
                 return  -1;
             }
         }
 
         zipClose(zFile,NULL);
         cout<< "zip ok" <<endl;
     }
     return  0;
}
1
2
3
4
5
6
7
8
9
10
target=mini
 
lib=-lminizip -laes -lz 
libpath=-L /usr/local/lib
incpath=-I /usr/local/include
${target}:
     g++ main.cpp -g -o $@ ${lib} ${libpath} ${incpath}
 
clean:
     - rm  ${target}









本文转自 hakuyo 51CTO博客,原文链接:http://blog.51cto.com/hakuyo/1958102,如需转载请自行联系原作者
目录
相关文章
|
6月前
|
存储 C++ Python
C++-筛选文件夹中符合要求的文件并拷贝出来(以手机号码查找为例)
C++-筛选文件夹中符合要求的文件并拷贝出来(以手机号码查找为例)
|
12月前
|
Java API Android开发
Android C++系列:访问Assets 文件夹.md
assets目录是Android的一种特殊目录,用于放置APP所需的固定文件,且该文件被打包到APK中时,不会被编码到二进制文件。 Android还存在一种放置在res下的raw目录,该目录与assets目录不同。
273 0
|
存储 文件存储 C++
C++ 实现输出某个文件夹下所有文件名称,finddata_t、findfirst、findnext函数祥讲细讲解
利用C++实现输出某个文件夹下的文件名,需要用到的函数及其数据类型;
|
算法 C++
使用VC++压缩解压缩文件夹
前言  项目中要用到一个压缩解压缩的模块, 看了很多文章和源代码, 都不是很称心, 现在把我自己实现的代码和大家分享. 要求:          1.使用Unicode(支持中文).        2.
1314 0
|
存储 C++
C++ 存储指定文件夹下的文件
  指定文件夹地址,然后读取文件夹内容。   file.h #include &lt;string&gt; #include &lt;vector&gt; #include &lt;io.h&gt; /* * 定义于io.h * 1、long _findfirst64i32(const char * _Filename,struct _finddata64i32_t * _FindDa
1188 0
|
4天前
|
存储 编译器 C语言
c++的学习之路:5、类和对象(1)
c++的学习之路:5、类和对象(1)
19 0
|
4天前
|
C++
c++的学习之路:7、类和对象(3)
c++的学习之路:7、类和对象(3)
19 0
|
3天前
|
设计模式 Java C++
【C++高阶(八)】单例模式&特殊类的设计
【C++高阶(八)】单例模式&特殊类的设计
|
3天前
|
编译器 C++
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象
【C++基础(八)】类和对象(下)--初始化列表,友元,匿名对象