Share c# 7zip example

duckienbrothers

New member
## C# 7ZIP Ví dụ

### 1. Giới thiệu

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ó là một công cụ mạnh mẽ có thể nén các tệp với kích thước nhỏ hơn, giúp chúng dễ dàng lưu trữ và chuyển.7-Zip cũng có thể trích xuất các tệp từ Lưu trữ và nó hỗ trợ nhiều định dạng tệp khác nhau.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách sử dụng C# để tạo kho lưu trữ 7 zip.Chúng tôi cũng sẽ chỉ cho bạn cách trích xuất các tệp từ kho lưu trữ 7 zip.

### 2. Tạo một kho lưu trữ 7 zip

Để tạo kho lưu trữ 7 zip, bạn có thể sử dụng lớp `zipfile`.Lớp `Zipfile` cung cấp các phương thức để tạo, thêm các tệp vào và trích xuất các tệp từ Lưu trữ.

Để tạo lưu trữ 7-zip mới, bạn có thể sử dụng phương thức `create ()`.Phương thức `created ()` đưa một đường dẫn đến tệp lưu trữ làm đối số đầu tiên của nó.Đối số thứ hai là một danh sách các tệp để thêm vào kho lưu trữ.

Mã sau đây cho thấy cách tạo kho lưu trữ 7 zip của các tệp `file1.txt` và` file2.txt`:

`` `C#
sử dụng System.io;
sử dụng Sevenzip.compression.lzma;

Không gian tên CSHARP7ZIPEXample
{
Chương trình lớp học
{
static void main (String [] args)
{
// Tạo một kho lưu trữ 7 zip mới.
Lưu trữ zipfile = new zipfile ("Archive.zip");

// Thêm tệp vào kho lưu trữ.
Archive.Addfile ("File1.txt");
Archive.Addfile ("File2.txt");

// Lưu kho lưu trữ.
Lưu trữ.save ();
}
}
}
`` `

### 3. Trích xuất các tệp từ kho lưu trữ 7 zip

Để trích xuất các tệp từ kho lưu trữ 7 zip, bạn có thể sử dụng phương thức `Trích xuất ()`.Phương thức `actact ()` đưa một đường dẫn đến tệp lưu trữ làm đối số đầu tiên của nó.Đối số thứ hai là một đường dẫn đến thư mục nơi bạn muốn trích xuất các tệp.

Mã sau đây cho thấy cách trích xuất các tệp từ kho lưu trữ `Archive.zip` vào thư mục` C: \ Temp`:

`` `C#
sử dụng System.io;
sử dụng Sevenzip.compression.lzma;

Không gian tên CSHARP7ZIPEXample
{
Chương trình lớp học
{
static void main (String [] args)
{
// Tạo một kho lưu trữ 7 zip mới.
Lưu trữ zipfile = new zipfile ("Archive.zip");

// Trích xuất các tập tin từ kho lưu trữ.
Archive.extract ("C: \ temp");
}
}
}
`` `

### 4. Kết luận

Trong hướng dẫn này, chúng tôi đã chỉ cho bạn cách sử dụng C# để tạo và trích xuất các tệp từ tài liệu lưu trữ 7-Zip.7-Zip là một công cụ mạnh mẽ có thể được sử dụng để nén và lưu trữ các tệp.Nó là một công cụ có giá trị cho bất kỳ nhà phát triển nào cần làm việc với các tệp có kích thước khác nhau.

### 5. Hashtags

* #C#
* #7zip
* #file nén
* #file lưu trữ
* #những công cụ phát triển
=======================================
## C# 7zip Example

### 1. Introduction

7-Zip is a free and open-source file archiver for Windows, macOS, Linux, and other operating systems. It is a powerful tool that can compress files to a smaller size, making them easier to store and transfer. 7-Zip can also extract files from archives, and it supports a wide variety of file formats.

In this tutorial, we will show you how to use C# to create a 7-Zip archive. We will also show you how to extract files from a 7-Zip archive.

### 2. Creating a 7-Zip Archive

To create a 7-Zip archive, you can use the `ZipFile` class. The `ZipFile` class provides methods for creating, adding files to, and extracting files from archives.

To create a new 7-Zip archive, you can use the `Create()` method. The `Create()` method takes a path to the archive file as its first argument. The second argument is a list of files to add to the archive.

The following code shows how to create a 7-Zip archive of the files `file1.txt` and `file2.txt`:

```c#
using System.IO;
using SevenZip.Compression.LZMA;

namespace CSharp7ZipExample
{
class Program
{
static void Main(string[] args)
{
// Create a new 7-Zip archive.
ZipFile archive = new ZipFile("archive.zip");

// Add files to the archive.
archive.AddFile("file1.txt");
archive.AddFile("file2.txt");

// Save the archive.
archive.Save();
}
}
}
```

### 3. Extracting Files from a 7-Zip Archive

To extract files from a 7-Zip archive, you can use the `Extract()` method. The `Extract()` method takes a path to the archive file as its first argument. The second argument is a path to the directory where you want to extract the files.

The following code shows how to extract the files from the archive `archive.zip` to the directory `C:\Temp`:

```c#
using System.IO;
using SevenZip.Compression.LZMA;

namespace CSharp7ZipExample
{
class Program
{
static void Main(string[] args)
{
// Create a new 7-Zip archive.
ZipFile archive = new ZipFile("archive.zip");

// Extract the files from the archive.
archive.Extract("C:\Temp");
}
}
}
```

### 4. Conclusion

In this tutorial, we showed you how to use C# to create and extract files from 7-Zip archives. 7-Zip is a powerful tool that can be used to compress and archive files. It is a valuable tool for any developer who needs to work with files of different sizes.

### 5. Hashtags

* #C#
* #7zip
* #file compression
* #file archiving
* #Developer tools
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top