This article will introduce how to use Golang language to decompress files.
Golang is a modern development language that is fast, efficient, safe, concise and reliable. Golang provides many standard libraries and their APIs to easily handle files and compressed files. In this article, we will use the archive/zip package from the standard library to unzip the file.
Before you start, you need to install the Golang development environment. You can download the installation package suitable for your operating system from the official website and install it.
You can easily decompress compressed files using archive/zip package. This package provides us with the ZipArchive type, which we can use to manipulate compressed files.
Add the following code to your Go file:
import ( "archive/zip" "fmt" "io" "os" )
To decompress the file, we need to open it. We can open a file using the os.Open() function, which will open a file reader if the file exists.
Add the following code to your Go file:
func main() { // 打开压缩文件 zipFile, err := os.Open("file.zip") if err != nil { panic(err) } defer zipFile.Close() // 创建文件的读取器 zipReader, err := zip.NewReader(zipFile, zipFile.Stat().Size()) if err != nil { panic(err) } }
In the above code, we first open the compressed file using the os.Open() function. If the os.Open() function returns an error, we use the panic() function to throw an exception. We next use the defer statement to close the file.
Next, we use the zip.NewReader() function to create a variable of type ZipReader. This function requires two parameters, the file reader and the file size. We get the file size through zipFile.Stat().Size(). If creating a variable of type ZipReader fails, we will use the panic() function to throw an exception.
Now that we have opened the compressed file and created a ZipReader, we can use it to decompress the file.
Add the following code to your Go file:
func main() { // 打开压缩文件 zipFile, err := os.Open("file.zip") if err != nil { panic(err) } defer zipFile.Close() // 创建文件的读取器 zipReader, err := zip.NewReader(zipFile, zipFile.Stat().Size()) if err != nil { panic(err) } // 解压缩文件 for _, file := range zipReader.File { _, err := os.Create(file.Name) if err != nil { panic(err) } defer file.Close() rc, err := file.Open() if err != nil { panic(err) } defer rc.Close() if file.FileInfo().IsDir() { os.Mkdir(file.Name, file.Mode()) } else { filePath := file.Name dir, _ := path.Split(filePath) os.MkdirAll(dir, file.Mode()) outFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode()) if err != nil { panic(err) } defer outFile.Close() _, err = io.Copy(outFile, rc) if err != nil { panic(err) } } } }
The above code uses a for loop to iterate through a list of files of type ZipReader. For each file, we create a new file using the os.Create() function. If the file already exists, a variable of type File will be returned. We close the file using defer statement.
Next, we use the file.Open() function to open the file. If opening the file fails, use the panic() function to throw an exception. Use the defer statement to close the file.
Then, we check if the file is a folder. If it is a folder, we use the os.Mkdir() function to create a new directory. Otherwise, we create the directory path using the os.MkdirAll() function. Finally, we create the output file using the os.OpenFile() function. We use the io.Copy() function to copy data from the input file to the output file.
The following is the complete code to decompress a file using Golang:
package main import ( "archive/zip" "fmt" "io" "os" "path" ) func main() { // 打开压缩文件 zipFile, err := os.Open("file.zip") if err != nil { panic(err) } defer zipFile.Close() // 创建文件的读取器 zipReader, err := zip.NewReader(zipFile, zipFile.Stat().Size()) if err != nil { panic(err) } // 解压缩文件 for _, file := range zipReader.File { _, err := os.Create(file.Name) if err != nil { panic(err) } defer file.Close() rc, err := file.Open() if err != nil { panic(err) } defer rc.Close() if file.FileInfo().IsDir() { os.Mkdir(file.Name, file.Mode()) } else { filePath := file.Name dir, _ := path.Split(filePath) os.MkdirAll(dir, file.Mode()) outFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode()) if err != nil { panic(err) } defer outFile.Close() _, err = io.Copy(outFile, rc) if err != nil { panic(err) } } } fmt.Println("解压成功!") }
In the above code, we first open a compressed file and create a new ZipReader. We then iterate through all the files in the ZipReader and create a new file or directory for each file. Finally, we copy the data from the source file and write it to the destination file. If any error occurs, we can either throw an exception through the panic() function.
Decompressing files using Golang is a very easy task. By using the archive/zip package from the standard library, we can easily handle compressed files. This article describes how to use the archive/zip package to open, read and decompress files. These techniques can help you better understand the file and compressed file processing of the Golang language.
The above is the detailed content of Golang implements decompression. For more information, please follow other related articles on the PHP Chinese website!