minizip 压缩文件夹C++完整代码 · The Chat Archiveminizip 压缩文件夹C++完整代码
Download ▾
Assistant
以下是使用minizip库在C++中压缩文件夹的完整示例代码:
windows下使用minizip压缩文件夹C++完整代码
Assistant
以下是在Windows下使用minizip库在C++中压缩文件夹的完整示例代码:
windows下使用Windows APi 和minizip压缩文件夹C++完整代码
Assistant
以下是在Windows下使用Windows API和minizip库在C++中压缩文件夹的完整示例代码:
windows下使用xzip压缩文件夹C++完整代码
Assistant
以下是在Windows下使用xzip库在C++中压缩文件夹的完整示例代码:
windows下使用xzip xunzip压缩文件夹C++完整代码
Assistant
#include <iostream>
#include <fstream>
#include <string>
#include "minizip/zip.h"
using namespace std;
// Recursive function to add contents of folder to zip
void addFolderToZip(zipFile zf, string path, string folderName)
{
// Open directory
DIR *dir;
dirent *ent;
if ((dir = opendir(path.c_str())) != NULL)
{
// Iterate over contents of directory
while ((ent = readdir (dir)) != NULL)
{
string filePath = path + "/" + ent->d_name;
// If directory, ignore . and .. entries
if (ent->d_type == DT_DIR)
{
if (string(ent->d_name) != "." && string(ent->d_name) != "..")
{
// Add subdirectory recursively
addFolderToZip(zf, filePath, folderName + "/" + ent->d_name);
}
}
// If file, add to zip
else if (ent->d_type == DT_REG)
{
// Open file
ifstream file(filePath, ios::in | ios::binary);
if (file.is_open())
{
// Write file to zip
zip_fileinfo info;
memset(&info, 0, sizeof(info));
zipOpenNewFileInZip(zf, (folderName + "/" + ent->d_name).c_str(), &info, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
char byte;
while (file.get(byte))
{
zipWriteInFileInZip(zf, &byte, 1);
}
zipCloseFileInZip(zf);
file.close();
}
}
}
closedir (dir);
}
}
int main()
{
// Open zip file
zipFile zf = zipOpen("myzip.zip", APPEND_STATUS_CREATE);
if (zf == NULL)
{
cout << "Failed to open zip file" << endl;
return -1;
}
// Add folder contents to zip
addFolderToZip(zf, "myfolder", "");
// Close zip file
zipClose(zf, NULL);
return 0;
}
该代码利用递归函数addFolderToZip遍历文件夹的内容,并将文件添加到zipFile对象中。addFolderToZip函数会先判断是文件还是文件夹,如果是文件夹,则递归调用自身处理子文件夹。如果是文件,则使用minizip提供的函数将文件添加到zip中。
在main函数中,我们首先创建一个zipFile对象,然后调用addFolderToZip将文件夹中的内容添加到zip中。最后关闭zipFile对象即可。
#include <iostream>
#include <fstream>
#include <string>
#include <direct.h>
#include "minizip/zip.h"
using namespace std;
// Recursive function to add contents of folder to zip
void addFolderToZip(zipFile zf, string path, string folderName)
{
// Open directory
_finddata_t fileInfo;
string findPath = path + "\\*";
intptr_t handle = _findfirst(findPath.c_str(), &fileInfo);
if (handle == -1)
{
cout << "Failed to open directory" << endl;
return;
}
// Iterate over contents of directory
do
{
string filePath = path + "\\" + fileInfo.name;
// If directory, ignore . and .. entries
if (fileInfo.attrib & _A_SUBDIR)
{
if (string(fileInfo.name) != "." && string(fileInfo.name) != "..")
{
// Add subdirectory recursively
addFolderToZip(zf, filePath, folderName + "/" + fileInfo.name);
}
}
// If file, add to zip
else
{
// Open file
ifstream file(filePath, ios::in | ios::binary);
if (file.is_open())
{
// Write file to zip
zip_fileinfo info;
memset(&info, 0, sizeof(info));
zipOpenNewFileInZip(zf, (folderName + "/" + fileInfo.name).c_str(), &info, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
char byte;
while (file.get(byte))
{
zipWriteInFileInZip(zf, &byte, 1);
}
zipCloseFileInZip(zf);
file.close();
}
}
} while (_findnext(handle, &fileInfo) == 0);
_findclose(handle);
}
int main()
{
// Open zip file
zipFile zf = zipOpen("myzip.zip", APPEND_STATUS_CREATE);
if (zf == NULL)
{
cout << "Failed to open zip file" << endl;
return -1;
}
// Add folder contents to zip
addFolderToZip(zf, "myfolder", "");
// Close zip file
zipClose(zf, NULL);
return 0;
}
该代码与前一个示例代码类似,不同之处在于使用了Windows系统下的文件遍历函数_findfirst和_findnext替代了前一个代码中使用的opendir和readdir。此外,在Windows下,路径使用的是反斜杠\而不是正斜杠/。
在Windows下,需要在代码开头包含direct.h头文件。
#include <iostream>
#include <fstream>
#include <string>
#include "minizip/zip.h"
#include <Windows.h>
using namespace std;
// Recursive function to add contents of folder to zip
void addFolderToZip(zipFile zf, string path, string folderName)
{
// Open directory
WIN32_FIND_DATA fileData;
HANDLE dirHandle = FindFirstFile((path + "\\*").c_str(), &fileData);
if (dirHandle == INVALID_HANDLE_VALUE)
{
cout << "Failed to open directory" << endl;
return;
}
// Iterate over contents of directory
do
{
string filePath = path + "\\" + fileData.cFileName;
// If directory, ignore . and .. entries
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (string(fileData.cFileName) != "." && string(fileData.cFileName) != "..")
{
// Add subdirectory recursively
addFolderToZip(zf, filePath, folderName + "/" + fileData.cFileName);
}
}
// If file, add to zip
else
{
// Open file
ifstream file(filePath, ios::in | ios::binary);
if (file.is_open())
{
// Write file to zip
zip_fileinfo info;
memset(&info, 0, sizeof(info));
zipOpenNewFileInZip(zf, (folderName + "/" + fileData.cFileName).c_str(), &info, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_DEFAULT_COMPRESSION);
char byte;
while (file.get(byte))
{
zipWriteInFileInZip(zf, &byte, 1);
}
zipCloseFileInZip(zf);
file.close();
}
}
} while (FindNextFile(dirHandle, &fileData) != 0);
FindClose(dirHandle);
}
int main()
{
// Open zip file
zipFile zf = zipOpen("myzip.zip", APPEND_STATUS_CREATE);
if (zf == NULL)
{
cout << "Failed to open zip file" << endl;
return -1;
}
// Add folder contents to zip
addFolderToZip(zf, "myfolder", "");
// Close zip file
zipClose(zf, NULL);
return 0;
}
该代码利用Windows API中的FindFirstFile和FindNextFile函数遍历文件夹的内容,并将文件添加到zipFile对象中。addFolderToZip函数会先判断是文件还是文件夹,如果是文件夹,则递归调用自身处理子文件夹。如果是文件,则使用minizip提供的函数将文件添加到zip中。
在main函数中,我们首先创建一个zipFile对象,然后调用addFolderToZip将文件夹中的内容添加到zip中。最后关闭zipFile对象即可。
#include <iostream>
#include <fstream>
#include <string>
#include "xzip/xzip.h"
using namespace std;
// Recursive function to add contents of folder to zip
void addFolderToZip(xzip::archive &zipArchive, string path, string folderName)
{
// Open directory
WIN32_FIND_DATA fileData;
HANDLE dirHandle = FindFirstFile((path + "\\*").c_str(), &fileData);
if (dirHandle == INVALID_HANDLE_VALUE)
{
cout << "Failed to open directory" << endl;
return;
}
// Iterate over contents of directory
do
{
string filePath = path + "\\" + fileData.cFileName;
// If directory, ignore . and .. entries
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (string(fileData.cFileName) != "." && string(fileData.cFileName) != "..")
{
// Add subdirectory recursively
addFolderToZip(zipArchive, filePath, folderName + "/" + fileData.cFileName);
}
}
// If file, add to zip
else
{
// Open file
ifstream file(filePath, ios::in | ios::binary);
if (file.is_open())
{
// Write file to zip
zipArchive.add_file(file, folderName + "/" + fileData.cFileName);
file.close();
}
}
} while (FindNextFile(dirHandle, &fileData) != 0);
FindClose(dirHandle);
}
int main()
{
// Open zip file
xzip::archive zipArchive;
if (!zipArchive.open("myzip.zip", xzip::archive::open_mode::write))
{
cout << "Failed to open zip file" << endl;
return -1;
}
// Add folder contents to zip
addFolderToZip(zipArchive, "myfolder", "");
// Close zip file
zipArchive.close();
return 0;
}
该代码使用xzip库中的xzip::archive类来创建和操作zip文件。addFolderToZip函数同前面的示例,使用Windows API中的FindFirstFile和FindNextFile函数遍历文件夹的内容,并将文件添加到xzip::archive对象中。
在main函数中,我们首先创建一个xzip::archive对象,并调用open函数将其打开。然后调用addFolderToZip将文件夹中的内容添加到zip中。最后关闭xzip::archive对象即可。
注意,需要在代码中包含xzip库的头文件,并将xzip库链接到你的程序中。关于如何使用和安装xzip库,请参考xzip库的官方文档。
以下是在Windows下使用xzip库和xunzip库在C++中压缩、解压文件夹的完整示例代码:
code
#include <iostream>
#include <fstream>
#include <string>
#include "xzip/xzip.h"
#include "xunzip/xunzip.h"
using namespace std;
// Recursive function to add contents of folder to zip
void addFolderToZip(xzip::archive &zipArchive, string path, string folderName)
{
// Open directory
WIN32_FIND_DATA fileData;
HANDLE dirHandle = FindFirstFile((path + "\\*").c_str(), &fileData);
if (dirHandle == INVALID_HANDLE_VALUE)
{
cout << "Failed to open directory" << endl;
return;
}
// Iterate over contents of directory
do
{
string filePath = path + "\\" + fileData.cFileName;
// If directory, ignore . and .. entries
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (string(fileData.cFileName) != "." && string(fileData.cFileName) != "..")
{
// Add subdirectory recursively
addFolderToZip(zipArchive, filePath, folderName + "/" + fileData.cFileName);
}
}
// If file, add to zip
else
{
// Open file
ifstream file(filePath, ios::in | ios::binary);
if (file.is_open())
{
// Write file to zip
zipArchive.add_file(file, folderName + "/" + fileData.cFileName);
file.close();
}
}
} while (FindNextFile(dirHandle, &fileData) != 0);
FindClose(dirHandle);
}
int main()
{
// Create zip file
xzip::archive zipArchive;
if (!zipArchive.open("myzip.zip", xzip::archive::open_mode::write))
{
cout << "Failed to create zip file" << endl;
return -1;
}
// Add folder contents to zip
addFolderToZip(zipArchive, "myfolder", "");
// Close zip file
zipArchive.close();
// Extract zip file
xunzip::extractor zipExtractor;
if (!zipExtractor.open("myzip.zip"))
{
cout << "Failed to open zip file" << endl;
return -1;
}
// Extract contents to folder
zipExtractor.extract_all("extracted");
// Close zip extractor
zipExtractor.close();
return 0;
}
该代码使用xzip库中的xzip::archive类创建和操作zip文件,并使用xunzip库中的xunzip::extractor类来解压zip文件。addFolderToZip函数同前面的示例,使用Windows API中的FindFirstFile和FindNextFile函数遍历文件夹的内容,并将文件添加到xzip::archive对象中。
在main函数中,我们首先创建一个xzip::archive对象,并调用open函数将其打开。然后调用addFolderToZip将文件夹中的内容添加到zip中。接着,我们创建一个xunzip::extractor对象,并调用其open函数打开zip文件。然后调用extract_all将zip文件中的内容解压到指定的文件夹中。最后关闭xunzip::extractor对象即可。
注意,需要在代码中同时包含xzip库和xunzip库的头文件,并将它们链接到你的程序中。关于如何使用和安装xzip库和xunzip库,请参考它们的官方文档。
Not yet included in a published release.