c++,如何用7z.dll解压tar.gz压缩包
时间: 2024-03-05 18:51:31 浏览: 160
使用7z.dll解压tar.gz压缩包可以使用C++语言。以下是使用7z.dll解压tar.gz压缩包的代码示例:
```c++
#include "7z.h"
#include "7zAlloc.h"
#include "7zCrc.h"
#include "7zFile.h"
#include "7zVersion.h"
int ExtractTarGZ(const wchar_t* archivePath, const wchar_t* outputFolder)
{
int result = 0;
HANDLE archiveFile = NULL;
HANDLE outputFile = NULL;
wchar_t* archiveFilePath = NULL;
wchar_t* outputFolderPath = NULL;
wchar_t* itemPath = NULL;
SRes res = SZ_OK;
CLookToRead2 lookStream;
CSzArEx db;
ISzAlloc allocImp;
ISzAlloc allocTempImp;
allocImp.Alloc = SzAlloc;
allocImp.Free = SzFree;
allocTempImp.Alloc = SzAllocTemp;
allocTempImp.Free = SzFreeTemp;
CrcGenerateTable();
archiveFilePath = (wchar_t*)malloc(sizeof(wchar_t) * (wcslen(archivePath) + 1));
wcscpy_s(archiveFilePath, wcslen(archivePath) + 1, archivePath);
outputFolderPath = (wchar_t*)malloc(sizeof(wchar_t) * (wcslen(outputFolder) + 1));
wcscpy_s(outputFolderPath, wcslen(outputFolder) + 1, outputFolder);
archiveFile = CreateFileW(archiveFilePath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (archiveFile == INVALID_HANDLE_VALUE)
{
result = GetLastError();
goto done;
}
lookStream.s.Read = SzFileRead;
lookStream.s.Seek = SzFileSeek;
lookStream.pos = 0;
lookStream.size = 0;
lookStream.object = archiveFile;
lookStream.buf = (Byte*)malloc(kInputBufSize);
SzArEx_Init(&db);
res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp);
if (res != SZ_OK)
{
result = res;
goto done;
}
for (UInt32 i = 0; i < db.NumFiles; i++)
{
size_t offset = 0;
size_t outSizeProcessed = 0;
size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL);
itemPath = (wchar_t*)malloc(sizeof(wchar_t) * len);
SzArEx_GetFileNameUtf16(&db, i, itemPath);
if (wcsstr(itemPath, L".tar") != NULL || wcsstr(itemPath, L".gz") != NULL)
{
continue;
}
wchar_t* fullOutputPath = (wchar_t*)malloc(sizeof(wchar_t) * (wcslen(outputFolderPath) + wcslen(itemPath) + 2));
wcscpy_s(fullOutputPath, wcslen(outputFolderPath) + 1, outputFolderPath);
wcscat_s(fullOutputPath, wcslen(outputFolderPath) + wcslen(itemPath) + 2, L"\\");
wcscat_s(fullOutputPath, wcslen(outputFolderPath) + wcslen(itemPath) + 2, itemPath);
if (db.Blocks[i].UnpackSize == 0)
{
outputFile = CreateFileW(fullOutputPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (outputFile == INVALID_HANDLE_VALUE)
{
result = GetLastError();
goto done;
}
CloseHandle(outputFile);
continue;
}
Byte* outBuffer = (Byte*)malloc(db.Blocks[i].UnpackSize);
res = SzArEx_Extract(&db, &lookStream.s, i, &offset, outBuffer, db.Blocks[i].UnpackSize, &outSizeProcessed, &allocTempImp, &allocImp);
if (res != SZ_OK)
{
result = res;
free(outBuffer);
goto done;
}
outputFile = CreateFileW(fullOutputPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (outputFile == INVALID_HANDLE_VALUE)
{
result = GetLastError();
free(outBuffer);
goto done;
}
DWORD bytesWritten = 0;
WriteFile(outputFile, outBuffer, outSizeProcessed, &bytesWritten, NULL);
CloseHandle(outputFile);
free(outBuffer);
free(fullOutputPath);
free(itemPath);
}
done:
if (archiveFile != NULL)
{
CloseHandle(archiveFile);
}
if (lookStream.buf != NULL)
{
free(lookStream.buf);
}
if (archiveFilePath != NULL)
{
free(archiveFilePath);
}
if (outputFolderPath != NULL)
{
free(outputFolderPath);
}
SzArEx_Free(&db, &allocImp);
CrcFreeTable();
return result;
}
```
其中,`archivePath`为tar.gz压缩包的完整路径,`outputFolder`为解压缩后文件的保存目录。这段代码会将压缩包中的所有文件解压到`outputFolder`目录中,忽略`.tar`和`.gz`文件。
阅读全文