Share c++ 7z

#C ++ #7Z #compression #Archive #file
## C ++ 7Z: Cách nén và trích xuất các tệp

C ++ 7Z là một thư viện cung cấp một cách đơn giản và hiệu quả để nén và trích xuất các tệp.Nó dựa trên thuật toán nén 7Z, là một trong những thuật toán nén mạnh nhất và hiệu quả nhất có sẵn.

Để sử dụng C ++ 7Z, bạn có thể sử dụng giao diện dòng lệnh hoặc API C ++.Giao diện dòng lệnh rất đơn giản để sử dụng, nhưng nó không cung cấp nhiều sự linh hoạt.API C ++ phức tạp hơn, nhưng nó cho phép bạn kiểm soát nhiều hơn đối với quá trình nén và trích xuất.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách sử dụng API C ++ để nén và trích xuất các tệp.Chúng tôi cũng sẽ cung cấp một số mẹo về cách tối ưu hóa mã nén và mã trích xuất của bạn.

### Tệp nén với C ++ 7Z

Để nén một tệp với C ++ 7Z, bạn có thể sử dụng mã sau:

`` `C ++
#include <Istream>
#include <7z.h>

int main () {
// Tạo đối tượng lưu trữ 7Z.
Lưu trữ Carchive;

// Thêm tệp để được nén vào kho lưu trữ.
Archive.addfile ("File.txt");

// Đặt mức nén.
Lưu trữ.SetLevel (9);

// Nén lưu trữ.
Archive.compress ();

// Nhận kích thước nén của kho lưu trữ.
size_t nénssize = Archive.getSize ();

// In kích thước nén của kho lưu trữ.
std :: cout << "Kích thước nén:" << TrumpedSize << std :: endl;

// Đóng kho lưu trữ.
Lưu trữ.Close ();

trả lại 0;
}
`` `

Mã này sẽ tạo một kho lưu trữ được nén có tên là `file.7z`.Kích thước nén của kho lưu trữ sẽ nhỏ hơn kích thước ban đầu của tệp.

### Trích xuất tệp với C ++ 7Z

Để trích xuất một tệp từ kho lưu trữ C ++ 7Z, bạn có thể sử dụng mã sau:

`` `C ++
#include <Istream>
#include <7z.h>

int main () {
// Tạo đối tượng lưu trữ 7Z.
Lưu trữ Carchive;

// Mở kho lưu trữ.
Archive.open ("Tệp.7z");

// Nhận danh sách các tập tin trong kho lưu trữ.
COBjectVector <CarchiveFile> files = Archive.getFiles ();

// Trích xuất tập tin đầu tiên trong kho lưu trữ.
Carchivefile file = files [0];
file.extract ("trích xuất.txt");

// Đóng kho lưu trữ.
Lưu trữ.Close ();

trả lại 0;
}
`` `

Mã này sẽ trích xuất tệp `Trích xuất.txt` từ kho lưu trữ` Tệp.7z`.

### Mẹo để tối ưu hóa mã nén và trích xuất của bạn

Dưới đây là một số mẹo để tối ưu hóa mã nén và trích xuất của bạn:

* Sử dụng phương thức `setLevel ()` để đặt mức nén.Mức nén càng cao, kích thước nén của kho lưu trữ càng nhỏ, nhưng càng mất càng lâu để nén và trích xuất kho lưu trữ.
* Sử dụng phương thức `setDictionAdeSize ()` để đặt kích thước từ điển.Kích thước từ điển càng lớn, nén càng tốt, nhưng càng nhiều bộ nhớ sẽ sử dụng.
* Sử dụng phương thức `setNumThreads ()` để đặt số lượng luồng để sử dụng để nén và trích xuất.Bạn càng sử dụng nhiều chủ đề, quá trình nén và chiết càng nhanh.

Bằng cách làm theo các mẹo này, bạn có thể tối ưu hóa mã nén và trích xuất cho các nhu cầu cụ thể của bạn.

## hashtags

* #C ++
* #7Z
* #Nén
* #lưu trữ
* #tài liệu
=======================================
#C++ #7z #compression #Archive #file
## C++ 7z: How to Compress and Extract Files

C++ 7z is a library that provides a simple and efficient way to compress and extract files. It is based on the 7z compression algorithm, which is one of the most powerful and efficient compression algorithms available.

To use C++ 7z, you can either use the command-line interface or the C++ API. The command-line interface is simple to use, but it does not provide much flexibility. The C++ API is more complex, but it gives you more control over the compression and extraction process.

In this tutorial, we will show you how to use the C++ API to compress and extract files. We will also provide some tips on how to optimize your compression and extraction code.

### Compressing Files with C++ 7z

To compress a file with C++ 7z, you can use the following code:

```c++
#include <iostream>
#include <7z.h>

int main() {
// Create a 7z archive object.
CArchive archive;

// Add the file to be compressed to the archive.
archive.AddFile("file.txt");

// Set the compression level.
archive.SetLevel(9);

// Compress the archive.
archive.Compress();

// Get the compressed size of the archive.
size_t compressedSize = archive.GetSize();

// Print the compressed size of the archive.
std::cout << "Compressed size: " << compressedSize << std::endl;

// Close the archive.
archive.Close();

return 0;
}
```

This code will create a compressed archive called `file.7z`. The compressed size of the archive will be smaller than the original size of the file.

### Extracting Files with C++ 7z

To extract a file from a C++ 7z archive, you can use the following code:

```c++
#include <iostream>
#include <7z.h>

int main() {
// Create a 7z archive object.
CArchive archive;

// Open the archive.
archive.Open("file.7z");

// Get the list of files in the archive.
CObjectVector<CArchiveFile> files = archive.GetFiles();

// Extract the first file in the archive.
CArchiveFile file = files[0];
file.Extract("extracted.txt");

// Close the archive.
archive.Close();

return 0;
}
```

This code will extract the file `extracted.txt` from the archive `file.7z`.

### Tips for Optimizing Your Compression and Extraction Code

Here are some tips for optimizing your compression and extraction code:

* Use the `SetLevel()` method to set the compression level. The higher the compression level, the smaller the compressed size of the archive, but the longer it will take to compress and extract the archive.
* Use the `SetDictionarySize()` method to set the dictionary size. The larger the dictionary size, the better the compression, but the more memory it will use.
* Use the `SetNumThreads()` method to set the number of threads to use for compression and extraction. The more threads you use, the faster the compression and extraction process will be.

By following these tips, you can optimize your compression and extraction code for your specific needs.

## Hashtags

* #C++
* #7z
* #compression
* #Archive
* #file
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top