使用方法:
创建压缩文件。可以指定压缩的文件保存在什么地方
//压缩文件路径和压缩文件名字,如:d:\file.zip string zip_file_name = dest_zip_dir + "\" + zip_name; TCHAR* t_zip_file_name = mystring.StringToPTCHAR(zip_file_name); hz = CreateZip(t_zip_file_name, 0);
ZipAddFolder 添加文件夹。
ZipAddFolder(hz, _T("format\label")); ZipAdd(hz, _T("format\label\barcode1.fmt"), _T("E:\ProgramTest\TScale_11DLLApplicationSample\Debug\ts_set\format\label\barcode1.fmt")); CloseZip(hz);
1.使用原文
2.建立控制台引用的时候会出现这样的错误"C2065:'HZIP': undeclared identifier“。
是因为控制台程序没添加#include<window.h> ,而且还要添加到zip.h前面。
3.原来解压缩中文名字的文件会有乱码,已被我修改好。
//by likewei comment out 压缩中文名字有乱码的问题 //#ifdef UNICODE // WideCharToMultiByte(CP_UTF8,0,dstzn,-1,zfi.iname,MAX_PATH,0,0); //#else // strcpy(zfi.iname,dstzn); //#endif //end comment out //by likewei add //TCHAR* -> char* int tchar_len = wcslen(dstzn); char *char_back = new char[tchar_len]; int byte_len = WideCharToMultiByte(CP_ACP, 0, dstzn, tchar_len, NULL, 0, NULL, NULL); WideCharToMultiByte(CP_ACP, 0, dstzn, tchar_len + 1, char_back, byte_len + 1, NULL, NULL); //char*-> char[] strcpy(zfi.iname,char_back); //end add
// zibtest.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <windows.h> #include <iostream> #include <vector> #include <string> #include "zip.h" #include "unzip.h" //string->TCHAR* TCHAR *StringToTcharP(std::string str) { //string -> char* char* p_char=const_cast<char*>(str.c_str()); //char* -> Tchar* int nLen = strlen(p_char) + 1; int nwLen = MultiByteToWideChar(CP_ACP,0,p_char,nLen,NULL, 0); //TCHAR p_tchar[256] 会乱码,更换成以下就好了 TCHAR * p_tchar=new TCHAR[256]; MultiByteToWideChar(CP_ACP, 0,p_char,nLen,p_tchar,nwLen); return p_tchar; } int _tmain(int argc, _TCHAR* argv[]) { HZIP hz; DWORD writ; std::string zip_file_name="E:\Projects\zlibTest2\ziptestfile\中国2.zip"; TCHAR *tchar_zip_file_name=StringToTcharP(zip_file_name); //hz = CreateZip(_T("E:\Projects\zlibTest2\ziptestfile\中国2.zip"),0); hz = CreateZip(tchar_zip_file_name,0); delete[]tchar_zip_file_name; //压缩文件 ZipAdd(hz,_T("中国.bmp"), _T("E:\Projects\zlibTest2\ziptestfile\中国.bmp")); ZipAdd(hz,_T("abc中国.bmp"), _T("E:\Projects\zlibTest2\ziptestfile\abc中国.bmp")); CloseZip(hz); //解压缩 hz = OpenZip(_T("d:\product_add_20150407111948.zip"),0); SetUnzipBaseDir(hz,_T("d:\")); ZIPENTRY ze; GetZipItem(hz,-1,&ze); int numitems=ze.index; for (int zi=0; zi<numitems; zi++) { GetZipItem(hz,zi,&ze); UnzipItem(hz,zi,ze.name); } CloseZip(hz); printf("zip is ok!"); system("pause"); return 0; }
压缩文件夹
int HaveSonDirectory(string source_dir) { string search_file_dir = source_dir + "\*.*"; _finddata_t fileInfo; long handle = _findfirst(search_file_dir.c_str(), &fileInfo); if (handle == -1L) { /*is_continue_search = false;*/ //return "failed to transfer files"; _findclose(handle); return -1; } string file_name; do { file_name = fileInfo.name; //判断是文件夹 if (fileInfo.attrib& _A_SUBDIR) { if (file_name.compare(0, 1, ".") != 0) { _findclose(handle); return 0; } } } while (_findnext(handle, &fileInfo) == 0); _findclose(handle); return -2; } int AddDirectoryToZip(HZIP& hz, string zip_name, string source_zip_dir,string current_zip_dir) { CMyString mystring; string file_name; string new_file_dir; string search_file_dir = source_zip_dir + "\*.*"; TCHAR* t_add_floder; TCHAR* t_zip_add_name; TCHAR* t_zip_add_name_path; _finddata_t fileInfo; long handle = _findfirst(search_file_dir.c_str(), &fileInfo); if (handle == -1L) { /*is_continue_search = false;*/ //return "failed to transfer files"; return 1; } do { file_name = fileInfo.name; //判断是文件夹 if (fileInfo.attrib& _A_SUBDIR) { if (file_name.compare(0, 1, ".") != 0) { new_file_dir = source_zip_dir + "\" + file_name; current_zip_dir = current_zip_dir+"\"+ file_name; if (HaveSonDirectory(new_file_dir) != 0) { //没有子目录,最后一层 current_zip_dir.erase(0, current_zip_dir.find_first_not_of("\")); t_add_floder = mystring.StringToPTCHAR(current_zip_dir); ZipAddFolder(hz, t_add_floder); delete[] t_add_floder; } AddDirectoryToZip(hz, zip_name, new_file_dir, current_zip_dir); int current_index = current_zip_dir.find("\"); if (current_index > 0) { current_zip_dir = current_zip_dir.substr(0, current_zip_dir.find_last_of("\")); } else { current_zip_dir = ""; } } } else { if (file_name != zip_name) { if (current_zip_dir == "") { t_zip_add_name = mystring.StringToPTCHAR(file_name); } else { t_zip_add_name = mystring.StringToPTCHAR(current_zip_dir + "\" + file_name); } t_zip_add_name_path = mystring.StringToPTCHAR(source_zip_dir + "\" + file_name); ZipAdd(hz, t_zip_add_name, t_zip_add_name_path); delete[] t_zip_add_name; delete[] t_zip_add_name_path; } } } while (_findnext(handle, &fileInfo)==0); _findclose(handle); return 0; } //zip_name 压缩文件名字,如:data.zip //source_zip_dir 要压缩文件的目录 如:d:\file //dest_zip_dir 保存压缩文件目录,此目录必须先创建 int CMyFile::AddZipDirectory(string zip_name, string source_zip_dir, string dest_zip_dir) { CMyString mystring; HZIP hz; //压缩文件路径和压缩文件名字,如:d:\file.zip string zip_file_name = dest_zip_dir + "\" + zip_name; TCHAR* t_zip_file_name = mystring.StringToPTCHAR(zip_file_name); hz = CreateZip(t_zip_file_name, 0); string current_zip_dir = ""; int ret = AddDirectoryToZip(hz, zip_name, source_zip_dir, current_zip_dir); delete[] t_zip_file_name; CloseZip(hz); return ret;