【问题标题】:DotNetZip, Powershell and Relative PathsDotNetZip、Powershell 和相对路径
【发布时间】:2011-12-07 05:16:06
【问题描述】:

我想将一些选定的文件夹压缩到仅包含其相对路径的存档中。

其中一些具有相同的文件夹名称(所有发布文件夹,所有调试文件夹)。

过滤工作正常,我在 get-children 命令之前调用了 set-location。

完成这项工作最简单的方法是什么?

我真的需要实现这样的东西吗

foreach ($o in $children)
{   $relPath = $o.FullName.Substring(subPath.Length);
    $relPath = $relPath.Substring(0, relPath.LastIndexOf(@"\"));
    zip.AddFolder($o.Name, $relPath);
} 

谁能给我一个例子?

谢谢

【问题讨论】:

  • 如果你能给出一些具体的例子来说明你想要做什么,那就太好了。问题很模糊

标签: powershell dotnetzip


【解决方案1】:

当我需要处理相对路径时,我会这样做:

$basePath = "C:\MyBasePath"
$newBasePAth = "C:\NewBasePath"

$files = Get-ChildItem $basePath
$newFileNames = foreach ($f in $files) {
    $f.Fullname.Replace($basePath, $newBasePath)
}

确保您使用的斜线模式在 $basePath 和 $newBasePath 中是相同的(使用 .Trim() 来确定)

希望对你有帮助

【讨论】:

  • 我想选择特殊文件(或文件夹)以包含在 zip 存档及其相对路径中。我正在尝试用 DotNetZip 做到这一点。
  • 我想选择特殊文件(或文件夹)以包含在 zip 存档中,它们的相对路径与我使用 set-location 设置的路径相对。我正在尝试使用 DotNetZip 来做到这一点。但总有一条完整的路径。所以我需要提取第一部分(就像winzip自动做的那样)。另一个问题是,当我包含完整的文件夹时。其中一些名称相同,但路径不同,我无法按名称添加第二个文件夹。
  • 这实际上是您应该只使用现有模块而不是编写自己的模块的情况之一。听起来您在包装 DotNetZip 和创建自己的提取命令时遇到问题。您可以查看 PowerShellPack(包括 Copy-ToZip,执行此操作)或 PowerShell Community Extensions (PSCX),其中包括更通用的 Expand-Archive(包括 .7z、.zip、.gzip、.iso 等) )
【解决方案2】:

我终于实现了,它适用于相对路径:

function ZipUp-Files ( $mychildren )
{
  foreach ($o in $mychildren) 
  {
        $e= $zipfile.AddDirectory($o.FullName,$o.fullname.substring($pwd.path.length));
  }
}

$children =get-childitem -recurse -force | where-object {$_.psiscontainer} | where {$_.name -match $teststring} 

[System.Reflection.Assembly]::LoadFrom("C:\dotNetZip\zip-v1.9\Release\Ionic.Zip.dll");
$zipfile =  new-object Ionic.Zip.ZipFile($ziptarget);
$zipfile.UseZip64WhenSaving= [Ionic.Zip.Zip64Option]::Always
$zipfile.MaxOutPutSegmentSize=734003200
ZipUp-Files $children
$zipfile.Save("c:\temp\arc_rel.zip")
$zipfile.Dispose()

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 2010-09-15
    • 2014-03-02
    • 1970-01-01
    • 2013-07-14
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多