AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 18872
Accepted
BlueGene
BlueGene
Asked: 2009-06-04 07:57:09 +0800 CST2009-06-04 07:57:09 +0800 CST 2009-06-04 07:57:09 +0800 CST

如何在 Powershell 中压缩/解压缩文件?

  • 772

是否有一种可以在 PowerShell 中压缩/解压缩文件 (*.zip) 的单行程序?

powershell command-line-interface compression
  • 11 11 个回答
  • 137582 Views

11 个回答

  • Voted
  1. Ameer Deen
    2010-11-14T17:36:43+08:002010-11-14T17:36:43+08:00

    这就是你可以在没有任何外部工具的情况下完全从 Powershell 中完成它的方法。这会将名为 test.zip 的文件解压缩到当前工作目录:

    $shell_app=new-object -com shell.application
    $filename = "test.zip"
    $zip_file = $shell_app.namespace((Get-Location).Path + "\$filename")
    $destination = $shell_app.namespace((Get-Location).Path)
    $destination.Copyhere($zip_file.items())
    
    • 154
  2. mousio
    2012-12-31T09:46:38+08:002012-12-31T09:46:38+08:00

    现在在 .NET Framework 4.5 中,有一个ZipFile类,您可以像这样使用它:

    [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem')
    [System.IO.Compression.ZipFile]::ExtractToDirectory($sourceFile, $targetFolder)
    
    • 56
  3. Best Answer
    Kevin Kuphal
    2009-06-04T08:02:35+08:002009-06-04T08:02:35+08:00

    DotNetZip将允许您从 PowerShell 执行此操作。它不是单行的,但该库将允许您编写所需的 PowerShell 脚本。

    您还可以使用 COM 接口,请参阅Compress Files with Windows PowerShell then package a Windows Vista Sidebar Gadget。

    谷歌搜索“zip powershell”或“unzip powershell”也可能会得到有用的结果。

    • 21
  4. Windos
    2015-10-18T12:59:07+08:002015-10-18T12:59:07+08:00

    我知道这是一个非常古老的问题,但我刚刚在 Twitter 上看到它链接,我想我会发布一个当前的答案。

    PowerShell 5 目前可在 Windows 10 上或通过Windows Management Framework 5 Production Preview获得,它带有两个用于“压缩”和“解压缩”的内置 cmdlet:

    • 压缩存档
    • 展开-存档
    • 19
  5. Kevin Colby
    2009-06-04T07:59:54+08:002009-06-04T07:59:54+08:00

    您可能希望查看具有专门为此的 cmdlet的 PowerShell 社区扩展 (PSCX) 。

    • 18
  6. Brian Reiter
    2009-08-04T11:40:29+08:002009-08-04T11:40:29+08:00

    我找到了最简单的解决方案,即只使用我多年来使用并在 UNIX 环境中使用的 infozip 二进制文件。

    PS> zip -9r ../test.zip * 
    PS> cd .. 
    PS> unzip -t test.zip Archive:  test.zip
        testing: LinqRepository/          OK
        testing: LinqRepository/ApplicationService.cs   OK
        testing: LinqRepository/bin/      OK 
    ... 
    No errors detected in compressed data of test.zip.
    

    在文本输出周围放置一个 powershell 包装器会很简单,但实际上我从来不需要它,所以我没有打扰。

    http://www.info-zip.org/

    • 5
  7. Nathan Hartley
    2011-08-12T07:07:40+08:002011-08-12T07:07:40+08:00

    我也喜欢Info-ZIP(大多数其他 Zip 实用程序中的 Zip 引擎)和7-Zip,这是另一个具有 GUI 和命令行 Zip 实用程序的最爱。重点是,有一些很好的命令行实用程序适用于大多数 PowerShell 任务。

    运行命令行实用程序有一些技巧,这些技巧不是使用 PowerShell 构建的:

    • 运行名称中以数字开头的可执行文件,在其前面加上与号 (&)。

      &7zip.exe

    • 将实用程序希望从命令行看到的每个标记用引号括起来。

      &"c:\path with space\SomeCommand.exe" "/parameter2" "/parameter2" "parameter2's Value" "Value2 `" with a quote"

    尝试这个:

    zip filename.zip (Get-ChildItem somepath\*)
    

    甚至:

    zip filename.zip (Get-Content ListOfFiles.txt)
    
    • 5
  8. Nico
    2013-10-12T09:56:57+08:002013-10-12T09:56:57+08:00

    詹姆斯霍尔威尔我喜欢你的回答,但我扩大了一点

    # Example
    #unzip "myZip.zip" "C:\Users\me\Desktop" "c:\mylocation"
    function unzip($fileName, $sourcePath, $destinationPath)
    {
        $shell = new-object -com shell.application
        if (!(Test-Path "$sourcePath\$fileName"))
        {
            throw "$sourcePath\$fileName does not exist" 
        }
        New-Item -ItemType Directory -Force -Path $destinationPath -WarningAction SilentlyContinue
        $shell.namespace($destinationPath).copyhere($shell.namespace("$sourcePath\$fileName").items()) 
    }
    
    • 2
  9. adopilot
    2009-08-04T11:49:02+08:002009-08-04T11:49:02+08:00

    WinRAR可以在 CMD 模式下工作,接受参数

    • 0
  10. Jonesome Reinstate Monica
    2015-01-14T14:53:48+08:002015-01-14T14:53:48+08:00

    离子方法摇滚:

    https://dotnetzip.codeplex.com/wikipage?title=PS-Examples

    支持密码,其他加密方法等。

    • 0

相关问题

  • Sharepoint 2007 的 Powershell 提供程序

  • 您最喜欢的用于系统管理的 Powershell 命令或脚本是什么?[关闭]

  • IIS 6.0 (Windows Server 2003) 上的 HTTP 压缩

  • 如何授予对 Exchange 2007 中每个人的日历的访问权限?

  • Vanilla Powershell 是否足以成为 Windows 和 DB 服务器管理员的语言?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    Windows 中执行反向 DNS 查找的命令行实用程序是什么?

    • 14 个回答
  • Marko Smith

    如何检查 Windows 机器上的端口是否被阻塞?

    • 4 个回答
  • Marko Smith

    我应该打开哪个端口以允许远程桌面?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    kch 如何更改我的私钥密码? 2009-08-06 21:37:57 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve