Golang compresses uploaded files into zip small case
This is a simple golang compressed file small case, can do a lot of expansion, the library used here is archive/zip, in the gopkg search zip on it.
Use the gin framework, get the front-end passed the file compressed into zip, storage path into the database, the code is simple example is as follows:
@Param file formData file true "Upload file" func UploadToZip(c *){ // Get the file passed by the front-end, if there is more than one file: use form,_ := () //files := ["file"] // Then iterate through the files to get to each one. file,err := ("file") if err != nil { ("The parameters are wrong.",c) return } // Create directories and specify filenames err = ( file ,"files/" + file + ) if err != nil { ("Failed to store file.",c) return } // Generate the zip file you want in the specified directory create, err := ("flies/") defer () if err != nil { ("Failed to create.",c) retuen } // Create a zip stream writer := (create) defer () // Read the stored directory and compress the files in it readFile ,err := ("files") if err != nil { ("Failed to read file.",c) retuen } // Iterate through directories to get individual files for _,rf := range readFile { //Compress only files, not directories. if !() { // Open the file to be compressed open,err := ("flies" + ()) if err != nil { ("Failed to open file.",c) retuen } //Create a compressed package inside the file and file name, so that after decompression there will be a files directory, directory inside the compressed files f, _ := ("flies" + ()) // Compress the file into a zip if _,err := (f,open);err != nil { ("Compression failed.",c) retuen } } } //Store compressed paths in the database ... ("Compression successful.",c) }
go language implementation of zip compressed file
Straight to the code:
func ZipFiles(filename string, files []string, oldform, newform string) error { newZipFile, err := (filename) if err != nil { return err } defer () zipWriter := (newZipFile) defer () // Add the files to the zip. for _, file := range files { zipfile, err := (file) if err != nil { return err } defer () info, err := () if err != nil { return err } header, err := (info) if err != nil { return err } = (file, oldform, newform, -1) = writer, err := (header) if err != nil { return err } if _, err = (writer, zipfile); err != nil { return err } } return nil } call (programming): ZipFiles(zipFileName, files, ".", ".")
Note: . indicates the root directory. If you need to put several files into the compressed file in which directory, then the third parameter of the function will be written, the above code indicates that the file to be compressed is in the root directory, the compressed file (the fourth parameter) will also be generated in the root directory.
to this article on how to Golang uploaded files compressed into zip (small case) of the article is introduced to this, more related to go uploaded files compressed zip content, please search for my previous posts or continue to browse the following related articles I hope that you will support me in the future more!