windows下面可以使用zip或者7z ,
7zip下载地址 https://www.7-zip.org/a/7z2408-x64.exe
假设我们有个文件夹里面有很多个文件夹,我们需要单独压缩每一个文件夹作为一个压缩文件 下面的简单命令可以帮助我们实现
下面几个简单的例子
进入到要压缩文件的目录执行
for /d %i in (*) do 7 z a "%i.zip" "%i"
如果使用bat批处理,需要把% 修改为%%
for /d %%i in (*) do 7 z a "%%i.zip" "%%i"
使用powershell的Compress-Archiv 实现
$sourcePath = "C:\Path\To\Your\Folders" $outputPath = "C:\Path\To\Save\CompressedFiles" Get-ChildItem -Path $sourcePath -Directory | ForEach-Object { $folderName = $_ .Name $zipFilePath = Join-Path -Path $outputPath -ChildPath "$folderName .zip" Compress-Archive -Path $_ .FullName -DestinationPath $zipFilePath }
linux下面压缩方式比较多,这里用个常用的压缩格式 tar.gz
#!/bin/bash folder_path="/path/to/your/folders" output_path="/path/to/save/compressed/files" for folder in "$folder_path " /*; do if [ -d "$folder " ]; then folder_name=$(basename "$folder " ) tar_file="$output_path /$folder_name .tar.gz" tar -czf "$tar_file " -C "$folder_path " "$folder_name " fi done