In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the usage of zip compression and zipdecompression examples in Node.js". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the usage of zip compression and zipdecompression examples in Node.js.
NodeJS File (folder) Compression / decompression Scheme (zip/unzip)-zip related commands on Linux
Use adm-zip
Adm-zip supports the functions of archive and unarchive for one or more files or entire folders, which is very easy to use.
Var adm_zip = require ("adm-zip"); / / creating archivesvar zip = new adm_zip (); zip.addLocalFolder (". / dist"); zip.writeZip (". / dist.zip"); / / extracting archivesvar unzip = new adm_zip ("dist.zip"); unzip.extractAllTo (". / dist", / * overwrite*/ true)
Use JSZip
When using this library, you need to add files to zip objects one by one, and you need to add the contents manually, and then use the write file operation to convert the zip objects in memory into physical storage. So if it's a whole folder, it's troublesome, and you need to traverse the folder.
Var JSZip = require ("jszip"); var fs = require ("fs"); var zip = new JSZip (); var file_content = fs.readFileSync ("archive/a.txt"); zip.file ("a.txt", file_content); var data = fs.readFileSync ("archive/img/pic.jpeg"); zip.file ("img/pic.jpeg", data, {base64: true}); var zipfolder = zip.generate ({type: "nodebuffer"}) Fs.writeFile ("jszip.zip", zipfolder, function (err) {if (err) throw err;})
There is also a folder method in JSZip, but it is only used to switch virtual paths within the zip object, such as zip.folder ("img"). File ('a.txt') is to add an IMG subdirectory to the zip and create an a.txt below, which is equivalent to zip.file ("img/a.txt"). It is also important to note that the contents of the file need to be added manually, if it is only zip.file ("a.txt"); only a txt file with empty content is created in the zip object, and it only exists in memory and needs to be written to disk before it is actually saved to disk.
More API https://github.com/Stuk/jszip
Use archiver and unzip
Archiver is powerful and supports zip format and tar format. You only need to provide a path to compress existing folders.
Compression:
/ / require modulesvar fs = require ("fs"); var archiver = require ("archiver"); / / create a file to stream archive data to.var output = fs.createWriteStream (_ _ dirname + "/ example.zip"); var archive = archiver ("zip", {zlib: {level: 9}, / / Sets the compression level.}); / / pipe archive data to the filearchive.pipe (output); / / append a file from streamvar file1 = _ dirname + "/ file1.txt" Archive.append (fs.createReadStream (file1), {name: "file1.txt"}); / / append a file from stringarchive.append ("string cheese!", {name: "file2.txt"}); / / append a file from buffervar buffer3 = Buffer.from ("buff it!"); archive.append (buffer3, {name: "file3.txt"}); / / append a filearchive.file ("file1.txt", {name: "file4.txt"}) / / append files from a sub-directory and naming it `new- subdir` within the archivearchive.directory ("subdir/", "new-subdir"); / / append files from a sub-directory, putting its contents at the root of archivearchive.directory ("subdir/", false); / / append files from a glob patternarchive.glob ("subdir/*.txt") / / finalize the archive (ie we are done appending files but streams have to finish yet) / / 'close',' end' or 'finish' may be fired right after calling this method so register to them beforehandarchive.finalize ()
Decompression
Var fs = require ("fs"); var unzip = require ("unzip"); fs.createReadStream ("archiver-unzip.zip") .pipe (unzip.Extract ({path: "unarchive"}))
More API https://github.com/archiverjs/node-archiver
The zip/unzip command apt-get install zip unzip on Linux
Extract the zip file
Grammar: unzip [- cflptuvz] [- agcjlmnoqsvx] [- P] [.zip file] [file] [- d] [- x] or unzip [- Z]
Supplementary note: unzip is an unzipped program for .zip compressed files.
Parameters:
-c displays the decompressed results on the screen and converts the characters appropriately.
-f update existing files.
-l displays the files contained in the compressed file.
The-p parameter is similar to the-c parameter and displays the result of the decompression to the screen, but no conversion is performed.
-t check that the compressed file is correct.
The-u parameter is similar to the-f parameter, but in addition to updating existing files, other files in the compressed file are also unzipped to a directory.
-v displays detailed information when executing Yes.
-z displays only the remarks text of the compressed file.
-a make the necessary character conversion to the text file.
-b do not convert characters to text files.
-the file name in a compressed C file is case sensitive.
-j does not process the original directory path in the compressed file.
-L changes all file names in the compressed file to lowercase.
-M sends the output to the more program for processing.
-n do not overwrite the original file when unzipping.
-o you do not need to ask the user first, the original file will be overwritten after unzip execution.
-P uses the password option of zip.
-Q does not display any information when executed.
-s converts white space characters in the file name to underscore characters.
-V retains the file version information of VMS.
-X unzips the original UID/GID of the file at the same time.
[.zip file] specifies the .zip zip file.
[file] specifies which files in the .zip zip file are to be processed.
-d specifies the directory to store after the file is unzipped.
-x specifies which files in the .zip zip file should not be processed.
-Z unzip-Z equals executing the zipinfo instruction.
The basic usage is: zip [parameter] [packaged file name] [packaged directory path]
Parameters:
-a convert the file to ASCII mode
-F attempts to repair corrupted compressed files
-h displays the help interface
-m after compressing the file, delete the source file
-n specific strings do not compress files with specific suffix strings
-O set the latest change time of all files in the compressed file to the time of compression.
-Q quiet mode, which does not show the execution of the instruction during compression
-r all subdirectories and files under the specified directory are processed together
-S contains system files and implicit files (S is uppercase)
Example:
The zip command can be used to compress files into the common zip format. The unzip command is used to extract the zip file. Zip file abc.txt and a directory dir1 with the name test.zip:
Zip-r test.zip abc.txt dir1# decompression: ```shunzip test.zip
There are abc1.zip,abc2.zip and abc3.zip in the directory, using wildcards
Unzip abc\? .zip
Note:? Represents one character, if any number of characters are represented by *. Do not decompress, just view the content
Unzip-v test.zip
Verify the completion of the zip file and check whether the bags downloaded online have been finished.
Unzip-t test.zip
I use the-v option to find that there are many directories and subdirectories in the music.zip zip file, and the subdirectories are actually songs mp3 files. I want to download these files to the first-level directory instead of building a directory layer by layer:
Unzip-j music.zip so far, I believe you have a deeper understanding of "zip compression and zipdecompression examples in Node.js". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.