Browse Source

#IQBX #comment remoteController 实体编译通过2

80374374 1 year ago
parent
commit
3452fb0dff
2 changed files with 247 additions and 0 deletions
  1. 213 0
      ThirdParty/Include/XZip/XZipZilb.cpp
  2. 34 0
      ThirdParty/Include/XZip/XZipZilb.h

+ 213 - 0
ThirdParty/Include/XZip/XZipZilb.cpp

@@ -0,0 +1,213 @@
+#include "XZipZilb.h"
+#include "libtoolkit/path.h"
+#include "fileutil.h"
+
+
+bool CreateZipFromFile(string srcFileName, string zipFileName)
+{
+	if (srcFileName.empty() || srcFileName.length() == 0 ||
+		zipFileName.empty() || zipFileName.length() == 0) {
+		//Dbg("压缩文件参数为空:");
+		Dbg("XZipZilb zip param is null");
+		return false;
+	}
+	int pos = srcFileName.find_last_of(SPLIT_SLASH);
+
+	if (pos < 0) {
+		//Dbg("文件路径不对:%s",srcFileName.c_str());
+		Dbg("XZipZilb srcFileName path format is error:%s", srcFileName.c_str());
+		return false;
+	}
+	string strName(srcFileName.substr(pos + 1));
+	// does zip source file exist?
+	//if (ExistsFileA(zipFileName)) {
+	//	remove(zipFileName.c_str();
+	//}
+
+	zipFile newZipFile = zipOpen(zipFileName.c_str(), APPEND_STATUS_CREATE); //创建zip文件
+	if (newZipFile == NULL)
+	{
+		//Dbg("无法创建zip文件!");
+		Dbg("XZipZilb can't create zip file: %s", zipFileName.c_str());
+		return false;
+	}
+	//压缩文件
+	if (!AddFileToZip(newZipFile, strName.c_str(), srcFileName.c_str()))
+	{
+		//Dbg("压缩失败: %s",zipFileName.c_str());
+		Dbg("XZipZilb zip fail: %s", zipFileName.c_str());
+		zipClose(newZipFile, NULL); 
+		if (ExistsFileA(zipFileName.c_str())) {
+			if (remove(zipFileName.c_str()) != 0) {
+				//Dbg("删除临时压缩文件失败");
+				Dbg("XZipZilb delete temp zip file is error");
+			}
+		}
+		return false;
+	}
+	//释放文件
+	zipClose(newZipFile, NULL); 
+	//Dbg("压缩成功: %s", zipFileName.c_str());
+	Dbg("XZipZilb zip succ: %s", zipFileName.c_str());
+	return true;
+	
+}
+
+bool AddFileToZip(zipFile zf, const char* fileNameInZip, const char* srcFile)
+{
+	//Dbg("fileNameInZip %s : %s", fileNameInZip, srcFile);
+	FILE* srcfp = NULL;
+
+	//初始化写入zip的文件信息(后期写入文件修改日期等信息)
+	zip_fileinfo zi;
+	zi.tmz_date.tm_sec = zi.tmz_date.tm_min = zi.tmz_date.tm_hour =
+		zi.tmz_date.tm_mday = zi.tmz_date.tm_mon = zi.tmz_date.tm_year = 0;
+
+	if (!insertZipFileEditTime(&zi, srcFile)) {
+		//插入文件修改时间失败
+		Dbg("XZipZilb create file editTime in zip fail: %s", srcFile);
+	}
+
+	zi.dosDate = 0;
+	zi.internal_fa = 0;
+	zi.external_fa = 0;
+
+	//如果srcFile为空,加入空目录
+	char new_file_name[256];
+	memset(new_file_name, 0, sizeof(new_file_name));
+	strcat(new_file_name, fileNameInZip);
+	if (srcFile == NULL)
+	{
+		strcat(new_file_name, SPLIT_SLASH_STR);
+	}
+
+	//在zip文件中创建新文件或文件夹
+	int ret = zipOpenNewFileInZip(zf, new_file_name, &zi, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
+	if (ret != ZIP_OK)
+	{
+		if (srcFile != NULL) {
+			//Dbg("在zip文件中创建新文件失败: %s", srcFile);
+			Dbg("XZipZilb create file in zip fail: %s", srcFile);
+		}
+		else {
+			//Dbg("在zip文件中创建新目录失败: %s", new_file_name);
+			Dbg("XZipZilb create dir in zip fail: %s", new_file_name);
+		}
+		return false;
+	}
+
+	if (srcFile != NULL)
+	{
+		//打开源文件
+		srcfp = fopen(srcFile, "rb+");
+		if (srcfp == NULL)
+		{
+			//Dbg("打开要往zip压缩的文件失败: %s", srcFile);
+			Dbg("XZipZilb open src file to zip is fail: %s", srcFile);
+			zipCloseFileInZip(zf); //关闭zip中的新文件
+			return false;
+		}
+
+		int fileLen = (int)GetFileLen(srcfp);
+		if (fseek(srcfp, 0, SEEK_SET)!=0) {
+			Dbg("XZipZilb fseek set begin fail: %s", srcFile);
+			fclose(srcfp);
+			zipCloseFileInZip(zf); //关闭zip中的新文件
+			return false;
+		}
+		//读入源文件并写入zip文件
+		//char buf[16 * 1024];
+		unsigned char* buf = new unsigned char[16*1024];
+		int numBytes = 0;
+		int sumWrite = 0;
+		while (!feof(srcfp) && !ferror(srcfp))
+		{
+			numBytes = fread(buf, 1, 16 * 1024, srcfp);
+			if (numBytes == 0) {
+				continue;
+			}
+			int ret = zipWriteInFileInZip(zf, buf, numBytes);
+			if (ret != ZIP_OK) {
+				Dbg("文件写入zip压缩包失败1: %s", srcFile);
+				fclose(srcfp);
+				zipCloseFileInZip(zf);
+				delete[] buf;
+				return false;//写文件失败
+			}
+			sumWrite += numBytes;
+		}
+
+		delete[] buf;
+
+		if (sumWrite != fileLen) {
+			//Dbg("文件写入zip压缩包失败,总长度不一致: %s", srcFile);
+			Dbg("XZipZilb write to zip file is error ,sumWrite is not equal to fileLen, %s", srcFile);
+			fclose(srcfp);
+			zipCloseFileInZip(zf);
+			return false;//写文件失败
+		}
+		//关闭源文件
+		fclose(srcfp);
+	}
+
+	//关闭zip文件
+	zipCloseFileInZip(zf);
+	return true;
+
+}
+
+long GetFileLen(FILE* _file)
+{
+	long curPosit = ftell(_file), fileLen;
+	fseek(_file, 0, SEEK_END);
+	fileLen = ftell(_file);
+	fseek(_file, curPosit, SEEK_SET);
+	return fileLen;
+}
+
+bool insertZipFileEditTime(zip_fileinfo* zi, const char* srcFile)
+{
+#ifdef RVC_OS_WIN
+	WIN32_FILE_ATTRIBUTE_DATA attr;
+	SYSTEMTIME filest;
+	FILETIME fttmp;
+	if (GetFileAttributesExA(srcFile, GetFileExInfoStandard, &attr))
+	{
+		if (FileTimeToLocalFileTime(&attr.ftLastWriteTime, &fttmp)) {
+			if (FileTimeToSystemTime(&fttmp, &filest)) {
+				zi->tmz_date.tm_sec = filest.wSecond;
+				zi->tmz_date.tm_min = filest.wMinute;
+				zi->tmz_date.tm_hour = filest.wHour;
+				zi->tmz_date.tm_mday = filest.wDay;
+				zi->tmz_date.tm_mon = filest.wMonth - 1;
+				zi->tmz_date.tm_year = filest.wYear - 1980;
+				return true;
+			}
+		}
+	}
+	return false;
+#else
+	struct stat attr_of_src;
+	if (lstat(srcFile, &attr_of_src) == 0)
+	{
+		time_t t = attr_of_src.st_mtime;
+		tm lt = { 0 };
+		localtime_r(&t, &lt);
+		zi->tmz_date.tm_sec = lt.tm_sec;
+		zi->tmz_date.tm_min = lt.tm_min;
+		zi->tmz_date.tm_hour = lt.tm_hour;
+		zi->tmz_date.tm_mday = lt.tm_mday;
+		zi->tmz_date.tm_mon = lt.tm_mon;
+		zi->tmz_date.tm_year = lt.tm_year+1900-1980;
+		return true;
+	}
+	return false;
+#endif
+}
+
+
+
+
+
+
+

+ 34 - 0
ThirdParty/Include/XZip/XZipZilb.h

@@ -0,0 +1,34 @@
+#ifndef RVC_MOD_UPLOAD_XZIP_ZLIB_H_
+#define RVC_MOD_UPLOAD_XZIP_ZLIB_H_
+
+#include "SpBase.h"
+
+#ifdef RVC_OS_WIN
+
+#define ZLIB_WINAPI //win32 必须使用的宏
+
+#endif 
+
+
+
+#include "zip.h"
+#include "unzip.h"
+#include <iostream>
+#include <stdio.h>
+#include <string>
+#ifdef RVC_OS_WIN
+#else
+#include <sys/stat.h>
+#endif
+using namespace std;
+
+bool CreateZipFromFile(string srcFileName, string zipFileName);
+
+bool AddFileToZip(zipFile zf, const char* fileNameInZip, const char* srcFile);
+//获取文件长度 完成
+long GetFileLen(FILE* _file);
+
+bool insertZipFileEditTime(zip_fileinfo* zi, const char* srcFile);
+
+
+#endif //RVC_MOD_UPLOAD_XZIP_ZLIB_H_