This article is a mirror article of machine translation, please click here to jump to the original article.

View: 11568|Reply: 2

[Source] [Actual combat]. NET/C# calls 7z decompression via the command line

[Copy link]
Posted on 2022-4-30 17:46:24 | | | |
Requirements: The project needs to decompress or compress the uploaded compressed package, which can generally be achieved through WinRAR, but we need to use programming methods to implement it, and it is impossible to manually decompress the file. Since WinRAR is chargeable, we can use .NET/C# to call 7z by performing command line operations to fulfill our needs.

Review:

.net/c# to get the file information in the zip package
https://www.itsvse.com/thread-4831-1-1.html

Java encapsulation zip unzip method
https://www.itsvse.com/thread-7750-1-1.html

C# uses ICSharpCode.SharpZipLib.dll to compress and decompress files/folders
https://www.itsvse.com/thread-3726-1-1.html
7-Zip is a file archiver with a high compression ratio. 7-Zip is open-source and freeware. Most of the code is licensed under the GNU LGPL. Some parts of the code are licensed under the BSD 3 Terms. Some parts of the code also have unRAR licensing restrictions.

Download Address:The hyperlink login is visible., the download and installation steps are omitted (this article is installed under the :D:\360Downloads directory)

The 7-Zip command line parameters are as follows:


7-Zip 21.07 (x64) : Copyright (c) 1999-2021 Igor Pavlov : 2021-12-26

Usage: 7z <command> [<switches>...] <archive_name> [<file_names>...] [@listfile]

<Commands>
  a : Add files to archive
  b : Benchmark
  d : Delete files from archive
  e : Extract files from archive (without using directory names)
  h : Calculate hash values for files
  i : Show information about supported formats
  l : List contents of archive
  rn : Rename files in archive
  t : Test integrity of archive
  u : Update files to archive
  x : eXtract files with full paths

<Switches>
  -- : Stop switches and @listfile parsing
  -ai[r[-|0]]{@listfile|! wildcard} : Include archives
  -ax[r[-|0]]{@listfile|! wildcard} : eXclude archives
  -ao{a|s|t|u} : set Overwrite mode
  -an : disable archive_name field
  -bb[0-3] : set output log level
  -bd : disable progress indicator
  -bs{o|e|p}{0|1|2} : set output stream for output/error/progress line
  -bt : show execution time statistics
  -i[r[-|0]]{@listfile|! wildcard} : Include filenames
  -m{Parameters} : set compression Method
    -mmt[N] : set number of CPU threads
    -mx[N] : set compression level: -mx1 (fastest) ... -mx9 (ultra)
  -o{Directory} : set Output directory
  -p{Password} : set Password
  -r[-|0] : Recurse subdirectories for name search
  -sa{a|e|s} : set Archive name mode
  -scc{UTF-8| WIN| DOS} : set charset for for console input/output
  -scs{UTF-8| UTF-16LE| UTF-16BE| WIN| DOS| {id}} : set charset for list files
  -scrc[CRC32| CRC64| SHA1| SHA256|*] : set hash function for x, e, h commands
  -sdel : delete files after compression
  -seml[.] : send archive by email
  -sfx[{name}] : Create SFX archive
  -si[{name}] : read data from stdin
  -slp : set Large Pages mode
  -slt : show technical information for l (List) command
  -snh : store hard links as links
  -snl : store symbolic links as links
  -sni : store NT security information
  -sns[-] : store NTFS alternate streams
  -so : write data to stdout
  -spd : disable wildcard matching for file names
  -spe : eliminate duplication of root folder for extract command
  -spf : use fully qualified file paths
  -ssc[-] : set sensitive case mode
  -sse : stop archive creating, if it can't open some input file
  -ssp : do not change Last Access Time of source files while archiving
  -ssw : compress shared files
  -stl : set archive timestamp from the most recently modified file
  -stm{HexMask} : set CPU thread affinity mask (hexadecimal number)
  -stx{Type} : exclude archive type
  -t{Type} : Set type of archive
  -u[-][p#][q#][r#][x#][y#][z#][!newArchiveName] : Update options
  -v{Size}[b|k|m|g] : Create volumes
  -w[{path}] : assign Work directory. Empty path means a temporary directory
  -x[r[-|0]]{@listfile|! wildcard} : eXclude filenames
  -y : assume Yes on all queries
Compress folders with 7z

The command is as follows:


Due to the addition of the -sdel parameter, the zh folder is automatically deleted when the compression is complete. Let's test the compressed file using WinRAR, as shown below:



Unzip the file with 7z

The command is as follows:


The decompression is completed as shown in the figure below:



.NET/C# calls 7z decompression via the command line

How do I use .NET/C# code to execute the command line? The code is as follows:

(End)




Previous:65 source code download sites
Next:[Actual combat]. NET/C# sets a timeout time for a method
 Landlord| Posted on 2022-4-30 17:49:29 |
e Unzip files (all compressed files are decompressed to the same directory, without maintaining the original directory structure)
# Unzip the file to the current directory
7z e archive.zip
# Unzip the file to the e:\testunzip directory
7z e archive.zip -oe:\testunzip
# Unzip all PNG files to the e:\testunzip directory
7z e archive.zip -oe:\testunzip *.png -r

x Unzip the file (keep the original directory structure)
# Unzip the file to the current directory
7z x archive.zip
# Unzip the file to the e:\testunzip directory
7z x archive.zip -oe:\testunzip
# Unzip all PNG files to the e:\testunzip directory
7z x archive.zip -oe:\testunzip *.png -r

l View a list of all files in the package
7z l archive.zip

-ao How files with the same name are handled
# -aoa overwrites the file of the same name
7z x archive.zip -aoa
# -aos Skip the file of the same name
# -aou renames the archive with the same name
# -aot renames files in the extracted directory with the same name

d Delete the files inside the package
# Delete all files with the .bak suffix in the archive.zip
7z d archive.zip *.bak -r

rn renames the file inside the package
# Rename the file 1.png 1_new.png 2.png folder\2_new.png
7z rn archive.7z 1.png 1_new.png 2.png folder\2_new.png
Posted on 2022-4-30 20:05:18 |
Learn to learn.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com