7z.dll,如何解压tar.gz压缩包
时间: 2023-11-01 07:22:35 浏览: 54
可以使用7-Zip软件中的7z.dll来解压缩tar.gz压缩包。以下是使用C#语言解压缩tar.gz压缩包的代码示例:
```csharp
using SevenZip.Compression.LZMA;
using System.IO;
public static void ExtractTarGZ(string filePath, string destinationPath)
{
using (FileStream fileStreamIn = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (FileStream fileStreamOut = new FileStream(destinationPath, FileMode.Create))
{
SevenZipExtractor extractor = new SevenZipExtractor(fileStreamIn);
extractor.ExtractFile("dummy", fileStreamOut);
}
}
}
```
其中,`filePath`为tar.gz压缩包的完整路径,`destinationPath`为解压缩后文件的保存路径。这段代码会将压缩包中的所有文件解压到`destinationPath`中。
相关问题
c++,如何用7z.dll解压tar.gz压缩包
使用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`文件。
如何用7z源码编译出来的7z.dll解压tar.gz压缩包。请用c++写一段代码实现
可以使用7z库中的解压函数来完成解压操作。下面是一个简单的示例代码,实现从tar.gz压缩包中解压出所有文件:
```c++
#include <iostream>
#include <Windows.h>
#include "7z.h"
#include "7zFile.h"
#include "7zAlloc.h"
int main() {
const wchar_t* archivepath = L"path/to/your/archive.tar.gz";
const wchar_t* outdir = L"path/to/output/directory";
HANDLE hArchive = CreateFileW(archivepath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hArchive == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to open archive: " << GetLastError() << std::endl;
return 1;
}
CFileInStream archiveStream;
archiveStream.file = hArchive;
archiveStream.s.Read = [](void* p, void* buf, size_t size, size_t* processedSize) -> SRes {
HANDLE handle = ((CFileInStream*)p)->file;
if (!ReadFile(handle, buf, (DWORD)size, (DWORD*)processedSize, NULL)) {
return SZ_ERROR_READ;
}
return SZ_OK;
};
CArchiveLink archiveLink;
archiveLink.InStream = &archiveStream.s;
archiveLink.InStream->Seek(&archiveStream.s, 0, SEEK_SET);
archiveLink.Arc.LibOpen = [](const wchar_t* name, int mode) -> void* {
return NULL; // no need to open shared library on Windows
};
archiveLink.Arc.LibClose = [](void* lib) -> int {
return 0; // no need to close shared library on Windows
};
archiveLink.Arc.LibSym = [](void* lib, const char* name) -> void* {
return NULL; // no need to get function address on Windows
};
ISzAlloc allocImp = { SzAlloc, SzFree };
ISzAlloc tempAllocImp = { SzAllocTemp, SzFreeTemp };
CMyComPtr<IInArchive> archive;
HRESULT hr = CreateObject(CLASS_CFormat7z, &IID_IInArchive, (void**)&archive);
if (hr != S_OK) {
std::cerr << "Failed to create archive object: " << hr << std::endl;
CloseHandle(hArchive);
return 1;
}
hr = archive->Open(&archiveLink.Arc, &allocImp, &tempAllocImp);
if (hr != S_OK) {
std::cerr << "Failed to open archive: " << hr << std::endl;
CloseHandle(hArchive);
return 1;
}
UInt32 numItems = 0;
hr = archive->GetNumberOfItems(&numItems);
if (hr != S_OK) {
std::cerr << "Failed to get number of items in archive: " << hr << std::endl;
CloseHandle(hArchive);
return 1;
}
for (UInt32 i = 0; i < numItems; i++) {
CMyComPtr<IArchiveExtractCallback> extractCallback = new CArchiveExtractCallback(outdir);
hr = archive->Extract(&i, 1, false, extractCallback);
if (hr != S_OK) {
std::cerr << "Failed to extract item " << i << " from archive: " << hr << std::endl;
}
}
archive->Close();
CloseHandle(hArchive);
return 0;
}
```
这段代码包含了7z库的头文件以及函数调用,需要在编译时链接7z库。可以使用以下命令来编译:
```
g++ -o extract extract.cpp -l7z
```
其中extract.cpp是保存上述代码的文件名,extract是可执行文件的名称。