Share c# 7-zip

organiczebra364

New member
## C#, 7-zip, zip, nén

# C# 7-Zip

C# là ngôn ngữ lập trình phổ biến để phát triển các ứng dụng Windows.7-ZIP là một tập tin lưu trữ tệp miễn phí và nguồn mở cho Windows, MacOS, Linux và các hệ điều hành khác.Nó có thể tạo và trích xuất tài liệu lưu trữ trong nhiều định dạng khác nhau, bao gồm zip, rar, 7Z và tar.

Trong bài viết này, chúng tôi sẽ chỉ cho bạn cách sử dụng C# để tạo và trích xuất tài liệu lưu trữ với 7-zip.Chúng tôi cũng sẽ cung cấp một số ví dụ mã mà bạn có thể sử dụng trong các dự án của riêng bạn.

### Tạo một kho lưu trữ với 7-zip

Để tạo một kho lưu trữ với 7-zip, bạn có thể sử dụng phương thức `createdearchive ()` của lớp `sevenzip.compression.zip`.Phương thức này lấy một danh sách các tệp hoặc thư mục làm đầu vào và tạo tệp lưu trữ 7Z.

Ví dụ: mã sau tạo tệp lưu trữ 7Z có tên là `myarchive.7z` chứa các tệp` file1.txt` và `file2.txt`:

`` `C#
sử dụng Sevenzip.compression.zip;

// Tạo một danh sách các tệp để bao gồm trong kho lưu trữ.
Danh sách <String> tập tin = danh sách mới <String> ();
file.add ("file1.txt");
file.add ("file2.txt");

// Tạo tệp lưu trữ 7Z mới.
Lưu trữ ziparchive = new ziparchive ();

// Thêm các tập tin vào kho lưu trữ.
Archive.Createarchive (tệp);

// Lưu kho lưu trữ vào đĩa.
Lưu trữ.save ("Viễn thị.7z");
`` `

### Trích xuất một kho lưu trữ với 7-zip

Để trích xuất một kho lưu trữ với 7-zip, bạn có thể sử dụng phương thức `extractarchive ()` của lớp `sevenzip.compression.zip`.Phương thức này lấy một tệp lưu trữ 7Z làm đầu vào và trích xuất các tệp và thư mục đến một vị trí được chỉ định.

Ví dụ: mã sau trích xuất nội dung của tệp lưu trữ 7Z `myarchive.7z` cho thư mục` C: \ temp`:

`` `C#
sử dụng Sevenzip.compression.zip;

// Tạo tệp lưu trữ 7Z mới.
Lưu trữ ziparchive = new ziparchive ();

// Trích xuất kho lưu trữ đến vị trí được chỉ định.
Lưu trữ.extractarchive ("myarchive.7z", "c: \ temp");
`` `

### Ví dụ về mã

Sau đây là một số ví dụ mã mà bạn có thể sử dụng để tạo và trích xuất tài liệu lưu trữ với 7-zip trong C#:

* [Tạo một kho lưu trữ với 7-zip] (https://gist.github.com/codewithmosh/b37914c9108433e05543b94e2519681e)
* [Trích xuất một kho lưu trữ với 7-zip] (https://gist.github.com/codewithmosh/237516252576e65709152f75b8db69b8)

### Tài nguyên

* [Tài liệu 7-zip] (https://www.7-zip.org/documentation.html)
* [C# 7-ZIP API tham chiếu] (https://sevenzip.osdn.net/chm/api/index.htm)
* [Diễn đàn 7-Zip] (https://forum.7-zip.org/)

### hashtags

* #C#
* #7-zip
* #Zip
* #Nén
* #lưu trữ
=======================================
## C#, 7-zip, Zip, Compression

# C# 7-zip

C# is a popular programming language for developing Windows applications. 7-zip is a free and open-source file archiver for Windows, macOS, Linux, and other operating systems. It can create and extract archives in a variety of formats, including ZIP, RAR, 7z, and TAR.

In this article, we will show you how to use C# to create and extract archives with 7-zip. We will also provide some code examples that you can use in your own projects.

### Creating an archive with 7-zip

To create an archive with 7-zip, you can use the `CreateArchive()` method of the `SevenZip.Compression.Zip` class. This method takes a list of files or directories as input and creates a 7z archive file.

For example, the following code creates a 7z archive file called `myarchive.7z` that contains the files `file1.txt` and `file2.txt`:

```c#
using SevenZip.Compression.Zip;

// Create a list of files to include in the archive.
List<string> files = new List<string>();
files.Add("file1.txt");
files.Add("file2.txt");

// Create a new 7z archive file.
ZipArchive archive = new ZipArchive();

// Add the files to the archive.
archive.CreateArchive(files);

// Save the archive to disk.
archive.Save("myarchive.7z");
```

### Extracting an archive with 7-zip

To extract an archive with 7-zip, you can use the `ExtractArchive()` method of the `SevenZip.Compression.Zip` class. This method takes a 7z archive file as input and extracts the files and directories to a specified location.

For example, the following code extracts the contents of the 7z archive file `myarchive.7z` to the directory `c:\temp`:

```c#
using SevenZip.Compression.Zip;

// Create a new 7z archive file.
ZipArchive archive = new ZipArchive();

// Extract the archive to the specified location.
archive.ExtractArchive("myarchive.7z", "c:\temp");
```

### Code examples

The following are some code examples that you can use to create and extract archives with 7-zip in C#:

* [Create an archive with 7-zip](https://gist.github.com/codewithmosh/b37914c9108433e05543b94e2519681e)
* [Extract an archive with 7-zip](https://gist.github.com/codewithmosh/237516252576e65709152f75b8db69b8)

### Resources

* [7-zip documentation](https://www.7-zip.org/documentation.html)
* [C# 7-zip API reference](https://sevenzip.osdn.net/chm/api/index.htm)
* [7-zip forum](https://forum.7-zip.org/)

### Hashtags

* #C#
* #7-zip
* #Zip
* #compression
* #Archive
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top