Go language compressed file processing
In modern application development, processing compressed files (such as .zip format) is a common requirement. The Go language provides a built-in archive/zip package to handle the reading and writing of .zip files, but sometimes we need to encapsulate some common operations to make the code more concise and easy to use. This article will introduce how to use Go to encapsulate a ziputil package to handle file compression and decompression operations.
1. Compressed file: Zip function
In Go, compressed files usually require the archive/zip package. We will traverse the folder or file, create a new .zip file, and add the files or folders one by one to the zip package.
package ziputil import ( "archive/zip" "go-admin/app/brush/utils" "sync" "io" "os" "path/filepath" log "/go-admin-team/go-admin-core/logger" ) // Zip compresses the specified folder or file into .zip filefunc Zip(source, zipFile string) error { // Create a new zip file zipFileWriter, err := (zipFile) if err != nil { return err } defer func(zipFileWriter *) { err := () if err != nil { ("Closing zip file failed: %s", err) } }(zipFileWriter) // Create a zip writer zipWriter := (zipFileWriter) defer func(zipWriter *) { err := () if err != nil { ("Closing zip writer failed: %s", err) } }(zipWriter) // Get the absolute path to the source file absSource, err := (source) if err != nil { return err } // traverse the folder and add it to the zip file return (absSource, func(path string, info , err error) error { if err != nil { return err } // Calculate the relative path of the file relPath, err := (absSource, path) if err != nil { return err } // If it is a directory, create a directory entry in the zip file if () { if relPath != "." { _, err := (relPath + "/") if err != nil { return err } } return nil } // Otherwise add the file to the zip file return addFileToZip(zipWriter, path, relPath) }) } // addFileToZip Adds a single file to the zip writerfunc addFileToZip(zipWriter *, file string, relPath string) error { f, err := (file) if err != nil { return err } defer func(f *) { err := () if err != nil { ("Failed to close file: %s", err) } }(f) // Create the file in the zip file writer, err := (relPath) if err != nil { return err } // Write the file content to zip _, err = (writer, f) if err != nil { return err } return nil }
2. Unzip the file: UnZip function
When decompressing the .zip file, we need to extract each file in the .zip file into the specified directory. The UnZip function can not only extract files, but also handle folder structures to ensure that the extracted directory structure is not lost.
// UnZip unzip zip file to the target directoryfunc UnZip(zipFile, destDir string) error { ("Decompress file: %s to %s", zipFile, destDir) r, err := (zipFile) if err != nil { return err } defer func(r *) { err := () if err != nil { ("Closing zip file failed: %s", err) } }(r) ("Total %d files", len()) // Concurrently decompress each file wg := {} for _, f := range { (1) go func(rf *, w *) { defer () if err := unzipFile(rf, destDir); err != nil { ("Decompress file [%s] failed: %v", , err) } }(f, &wg) } () return nil } // unzipFile decompresses a single file to the target directoryfunc unzipFile(f *, destDir string) error { // Convert file name to UTF-8 filename := utils.ConvertToUTF8([]byte()) filePath := (destDir, filename) // Create folder if ().IsDir() { return (filePath, ) } // Create the parent directory of the file if err := ((filePath), ); err != nil { ("Create directory [%s] failed: %v", (filePath), err) return err } // Open the file file, err := () if err != nil { ("Open file [%s] failed: %v", filePath, err) return err } defer func(file ) { err := () if err != nil { ("Close file [%s] failed: %v", filePath, err) } }(file) // Create a file outFile, err := (filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, ()) if err != nil { ("Create file [%s] failed: %v", filePath, err) return err } defer func(outFile *) { err := () if err != nil { ("Close file [%s] failed: %v", filePath, err) } }(outFile) // Write the file contents to _, err = (outFile, file) if err != nil { ("Copy file [%s] failed: %v", filePath, err) return err } return nil }
3. Summary
Through the ziputil package, we can easily compress and decompress files and folders. This package uses Go's built-in archive/zip package to process .zip files, and realizes concurrent processing of the compression process to improve compression efficiency. For larger compressed files or compressed packages containing large amounts of files, using concurrent processing can significantly improve performance.
This is the end of this article about implementing compressed file processing based on Go language. For more related Go compressed file content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!