Powershell实现创建zip压缩文件
发布日期:2021-08-19 16:01:00 浏览次数:1 分类:技术文章

本文共 2568 字,大约阅读时间需要 8 分钟。

      Powershell是运行于Windows平台上脚本,应用广泛。这里我们来实现ZIP压缩文件。首先,这里引用开源 ,所以您得先下载这个程序集。

把下面的内容写成一个CreateZipFile.ps1文件:

##############################################################################
##
## CreateZipFile
##
## by Peter Liu (http://wintersun.cnblogs.com)
##
##############################################################################
 
<#
 
.SYNOPSIS
 
Create a Zip file from any files piped in. Requires that
you have the SharpZipLib installed, which is available from
http://www.icsharpcode.net/OpenSource/SharpZipLib/
 
.EXAMPLE
 
dir testdata\*.txt | .\CreateZipFile data.zip h:\Dev\bin\ICSharpCode.SharpZipLib.dll
 
.EXAMPLE
 
"readme.txt" | .\CreateZipFile  docs.zip h:\Dev\bin\ICSharpCode.SharpZipLib.dll
Copies readme.txt to docs.zip
 
#>
 
param(
## The name of the zip archive to create
$ZipName = $(throw "Specify a zip file name"),
 
## The path to ICSharpCode.SharpZipLib.dll
$LibPath = $(throw "Specify the path to SharpZipLib.dll")
)
 
Set-StrictMode -Version Latest
 
## Load the Zip library
[void] [Reflection.Assembly]::LoadFile($libPath)
$namespace = "ICSharpCode.SharpZipLib.Zip.{0}"
 
## Create the Zip File
$zipName = $executionContext.SessionState.`
Path.GetUnresolvedProviderPathFromPSPath($zipName)
$zipFile =
New-Object ($namespace -f "ZipOutputStream") ([IO.File]::Create($zipName))
$zipFullName = (Resolve-Path $zipName).Path
 
[byte[]] $buffer = New-Object byte[] 4096
 
## Go through each file in the input, adding it to the Zip file
## specified
foreach($file in $input)
{
## Skip the current file if it is the zip file itself
if($file.FullName -eq $zipFullName)
{
continue
}
 
## Convert the path to a relative path, if it is under the
## current location
$replacePath = [Regex]::Escape( (Get-Location).Path + "\" )
$zipName = ([string] $file) -replace $replacePath,""
 
## Create the zip entry, and add it to the file
$zipEntry = New-Object ($namespace -f "ZipEntry") $zipName
$zipFile.PutNextEntry($zipEntry)
 
$fileStream = [IO.File]::OpenRead($file.FullName)
[ICSharpCode.SharpZipLib.Core.StreamUtils]::Copy(
$fileStream, $zipFile, $buffer)
$fileStream.Close()
}
 
## Close the file
$zipFile.Close()

如果您是第一次执行ps1文件在Powershell的控制台,还需要执行:

Set-ExecutionPolicy RemoteSigned

然后例如我们执行:

dir testdata\*.txt | .\CreateZipFile data.zip h:\Dev\bin\ICSharpCode.SharpZipLib.dll

把当目录下testdata文件夹中的所有txt文件压缩到data.zip文件。
软件工程中我们经常需要做自动化处理,使用脚本来压缩文件是常见的操作。

 

希望对您软件开发有帮助。

关于PowerShell,请看

作者:
出处:
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
该文章也同时发布在我的独立博客中-。

转载于:https://www.cnblogs.com/wintersun/archive/2012/11/30/2796827.html

转载地址:https://blog.csdn.net/weixin_30940783/article/details/97957774 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:重力控制网数据考点
下一篇:hdu-5497 Inversion(滑动窗口+树状数组)

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年03月12日 17时43分02秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

Java前台显示近20天的东西_第十次课:前台首页设计及显示商品信息 2019-04-21
java开发web网站的路由设计_理解Web路由(浅谈前后端路由与前后端渲染) 2019-04-21
excel如何把顺序倒过来_在excel中怎么使文字颠倒顺序反过来显示呢? 2019-04-21
java 62进制 转换_序列号生成的另一种玩法--62进制如何玩? 2019-04-21
php正则表达式获取图片路径,php 常用正则表达式实例(图片地址,与指定内容获取)... 2019-04-21
脚本语言php是什么意思,PHP脚本语言 2019-04-21
matlab数学规划模型,数学规划模型 2019-04-21
视频提取音频php,如何提取视频中的音频,从视频文件中提取出音频输出成MP3格式... 2019-04-21
diy.php添加验证码,织梦dedecms自定义表单中加入验证码 2019-04-21
在php脚本中 通过可以获取,在PHP中,可以使用Unix时间戳获取精确的脚本执行时间。... 2019-04-21
s2-045 php exp,S2-045-EXP.py --Struts2任意代码执行漏洞 (S2-045,CVE-2017-5638) 2019-04-21
linux sdk 窗口句柄,Venus: 针对Linux平台上,对常用的系统API进行面向对象的封装SDK。... 2019-04-21
c语言程序设计 科学出版社习题答案,C语言程序设计(科学出版社)第4章 课后习题参考答案.doc... 2019-04-21
c语言 无错 但只运行一半,求哈夫曼编码时程序运行到一半就终止了,编译无错... 2019-04-21
deepin linux 2014安装,2014.2版本的Deepin虚拟机安装浅谈(就是深度Linux) 2019-04-21
android 限速工具,Android下载器之限速篇 2019-04-21
html刷新ajax实现原理,AJAX的原理—如何做到异步和局部刷新 2019-04-21
html中列表菜单加文字请选择,html中下拉菜单 2019-04-21
读书郎平板中android,读书郎学生平板电脑怎么用 使用方法详解【图文】 2019-04-21
html5 调用摄像头 支持IE,JS调用本地摄像头拍照(兼容各大浏览器及IE8+) 2019-04-21