Share vb.net 7zip 解凍

### Cách giải nén các tệp 7Zip trong vb.net

** 1.Cài đặt thư viện 7ZIP **

Bước đầu tiên là cài đặt thư viện 7ZIP.Bạn có thể làm điều này bằng cách tải xuống thư viện 7ZIP từ [trang web 7ZIP] (https://www.7-zip.org/doad.html).Khi bạn đã tải xuống thư viện, hãy trích xuất các tệp vào một thư mục trên máy tính của bạn.

** 2.Thêm thư viện 7ZIP vào dự án của bạn **

Tiếp theo, bạn cần thêm thư viện 7ZIP vào dự án của mình.Để thực hiện việc này, nhấp chuột phải vào dự án của bạn trong Giải pháp Explorer và chọn ** Thêm tham chiếu **.Trong hộp thoại ** Trình quản lý tham chiếu **, nhấp vào nút ** Duyệt ** và điều hướng đến thư mục nơi bạn đã trích xuất các tệp thư viện 7ZIP.Chọn tệp ** 7zip.dll ** và nhấp vào nút ** Thêm **.

** 3.Giải nén tệp 7ZIP **

Bây giờ bạn có thư viện 7ZIP được thêm vào dự án của mình, bạn có thể giải nén tệp 7ZIP.Để thực hiện việc này, hãy tạo một `form` mới trong dự án của bạn và thêm mã sau vào trình xử lý sự kiện` form_load`:

`` `
Lưu trữ Dim dưới dạng Sevenzip.Archive () mới ()
Dim InputStream as stream = file.openread ("myfile.7z")
Archive.open (InputStream)

Dim đầu ra Dim dưới dạng chuỗi = "C: \ TEMP"
Dim OutputFileStream dưới dạng stream = file.create (path.combine (overputfolder, "myfile.txt")))
Archive.extractToStream (OutputFileStream)

outputfileStream.close ()
InputStream.close ()
`` `

Mã này sẽ giải nén tệp `myfile.7z` cho thư mục` c: \ temp`.Tệp `myfile.txt` sẽ được tạo trong thư mục` c: \ temp`.

**4.Giải nén tệp 7ZIP vào một thư mục cụ thể **

Bạn cũng có thể giải nén một tệp 7ZIP vào một thư mục cụ thể.Để thực hiện điều này, chỉ cần chỉ định đường dẫn thư mục là đối số thứ hai cho phương thức `extractToStream`.Ví dụ: mã sau sẽ giải nén tệp `myfile.7z` cho thư mục` C: \ temp \ myfiles`:

`` `
Lưu trữ Dim dưới dạng Sevenzip.Archive () mới ()
Dim InputStream as stream = file.openread ("myfile.7z")
Archive.open (InputStream)

Dim đầu ra Dim as String = "C: \ Temp \ MyFiles"
Dim OutputFileStream dưới dạng stream = file.create (path.combine (overputfolder, "myfile.txt")))
Archive.extractToStream (OutputFileStream)

outputfileStream.close ()
InputStream.close ()
`` `

** 5.Xử lý lỗi **

Có một vài điều có thể sai khi giải nén tệp 7ZIP.Ví dụ: tệp có thể bị hỏng hoặc người dùng có thể không có quyền truy cập thư mục đích.Nếu xảy ra lỗi, phương thức `extractOstoStream` sẽ ném một ngoại lệ.Bạn có thể bắt được ngoại lệ này và xử lý nó cho phù hợp.Ví dụ: mã sau sẽ hiển thị hộp thông báo nếu xảy ra lỗi khi giải nén tệp `myfile.7z`:

`` `
Lưu trữ Dim dưới dạng Sevenzip.Archive () mới ()
Dim InputStream as stream = file.openread ("myfile.7z")
Archive.open (InputStream)

Dim đầu ra Dim dưới dạng chuỗi = "C: \ TEMP"
Dim OutputFileStream dưới dạng stream = file.create (path.combine (overputfolder, "myfile.txt")))

Thử
Archive.extractToStream (OutputFileStream)
Bắt EX làm ngoại lệ
MessageBox.Show (Ex.Message)
Kết thúc thử

outputfileStream.close ()
InputStream.close ()
`` `

### hashtags

* #7zip
* #Unzip
* #vb.net
* #Zip
* #lưu trữ
=======================================
### How to unzip 7zip files in VB.NET

**1. Install the 7zip library**

The first step is to install the 7zip library. You can do this by downloading the 7zip library from the [7zip website](https://www.7-zip.org/download.html). Once you have downloaded the library, extract the files to a folder on your computer.

**2. Add the 7zip library to your project**

Next, you need to add the 7zip library to your project. To do this, right-click on your project in the Solution Explorer and select **Add Reference**. In the **Reference Manager** dialog box, click the **Browse** button and navigate to the folder where you extracted the 7zip library files. Select the **7zip.dll** file and click the **Add** button.

**3. Unzip a 7zip file**

Now that you have the 7zip library added to your project, you can unzip a 7zip file. To do this, create a new `Form` in your project and add the following code to the `Form_Load` event handler:

```
Dim archive As New SevenZip.Archive()
Dim inputStream As Stream = File.OpenRead("myfile.7z")
archive.Open(inputStream)

Dim outputFolder As String = "C:\Temp"
Dim outputFileStream As Stream = File.Create(Path.Combine(outputFolder, "myfile.txt"))
archive.ExtractToStream(outputFileStream)

outputFileStream.Close()
inputStream.Close()
```

This code will unzip the file `myfile.7z` to the folder `C:\Temp`. The file `myfile.txt` will be created in the `C:\Temp` folder.

**4. Unzip a 7zip file to a specific folder**

You can also unzip a 7zip file to a specific folder. To do this, simply specify the folder path as the second argument to the `ExtractToStream` method. For example, the following code will unzip the file `myfile.7z` to the folder `C:\Temp\myfiles`:

```
Dim archive As New SevenZip.Archive()
Dim inputStream As Stream = File.OpenRead("myfile.7z")
archive.Open(inputStream)

Dim outputFolder As String = "C:\Temp\myfiles"
Dim outputFileStream As Stream = File.Create(Path.Combine(outputFolder, "myfile.txt"))
archive.ExtractToStream(outputFileStream)

outputFileStream.Close()
inputStream.Close()
```

**5. Handling errors**

There are a few things that can go wrong when unzipping a 7zip file. For example, the file may be corrupt, or the user may not have permission to access the destination folder. If an error occurs, the `ExtractToStream` method will throw an exception. You can catch this exception and handle it accordingly. For example, the following code will display a message box if an error occurs when unzipping the file `myfile.7z`:

```
Dim archive As New SevenZip.Archive()
Dim inputStream As Stream = File.OpenRead("myfile.7z")
archive.Open(inputStream)

Dim outputFolder As String = "C:\Temp"
Dim outputFileStream As Stream = File.Create(Path.Combine(outputFolder, "myfile.txt"))

Try
archive.ExtractToStream(outputFileStream)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

outputFileStream.Close()
inputStream.Close()
```

### Hashtags

* #7zip
* #Unzip
* #vb.net
* #Zip
* #Archive
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top