如何在powershell中使用DotNetZip仅压缩文件而不是完整路径层次结构? [英] How to zip only files and not the full path hierarchy with DotNetZip in powershell?

查看:55
本文介绍了如何在powershell中使用DotNetZip仅压缩文件而不是完整路径层次结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 DotNetZip 和 powershell 压缩日志.文件位于 C:\user\temp\logs 当我遍历目录中的日志并将它们添加到 zip 文件时,当我只需要日志文件时,我最终会得到文件夹层次结构和日志文件.

所以 zip 最终包含:

-用户└温度└日志└log1.loglog2.loglog3.log

当我想要包含的 zip 文件是:

log1.loglog2.loglog3.log

这是我正在运行以进行测试的脚本:

[System.Reflection.Assembly]::LoadFrom("c:\\User\\bin\\Ionic.Zip.dll");$zipfile = new-object Ionic.Zip.ZipFile("C:\user\temp\logs\TestZIP.zip");$directory = "C:\user\temp\logs\"$children = get-childitem -path $directoryforeach ($o 在 $children 中){if($o.Name.EndsWith(".log")){$e = $zipfile.AddFile($o.FullName)}}$zipfile.Save()$zipfile.Dispose()

解决方案

有一个AddFile 您可以在其中覆盖存档中的文件名:

public ZipEntry AddFile(字符串文件名,字符串目录PathInArchive)

<块引用>

文件名(字符串)

要添加的文件名.文件名可能是相对的路径或完全限定的路径.

directoryPathInArchive (String)

指定用于覆盖文件名中的任何路径的目录路径.此路径可能对应,也可能不对应到当前的真实目录文件系统.如果文件中的zip 稍后被提取,这是用于提取文件的路径.传递 null(在 VB 中没有)将使用fileName 上的路径(如果有).传递空字符串 ("") 将在根路径插入项目在存档中.

试试这个:

 $e = $zipfile.AddFile($o.FullName, $o.Name)

这也可能是你想要的:

 $e = $zipfile.AddFile($o.FullName, "")

I'm trying to zip up log using DotNetZip and powershell. The files are in C:\user\temp\logs When I loop through the logs in the directory and add them to the zip file, I end up with the folder hierarchy and the log files when I only want the log files.

So the zip ends up containing:

-user
  └temp  
    └logs
       └log1.log
        log2.log
        log3.log

When I want the zip file to contain is:

log1.log
log2.log
log3.log

Here is the script I'm running to test with:

[System.Reflection.Assembly]::LoadFrom("c:\\\User\\bin\\Ionic.Zip.dll");
$zipfile = new-object Ionic.Zip.ZipFile("C:\user\temp\logs\TestZIP.zip");

$directory = "C:\user\temp\logs\"
$children = get-childitem -path $directory
foreach ($o in $children)
{
   if($o.Name.EndsWith(".log")){
      $e = $zipfile.AddFile($o.FullName)
   }
}
$zipfile.Save()
$zipfile.Dispose()

解决方案

There is an AddFile where you can override the filename in the archive:

public ZipEntry AddFile(
    string fileName,
    string directoryPathInArchive
)

fileName (String)

The name of the file to add. The name of the file may be a relative path or a fully-qualified path.

directoryPathInArchive (String)

Specifies a directory path to use to override any path in the fileName. This path may, or may not, correspond to a real directory in the current filesystem. If the files within the zip are later extracted, this is the path used for the extracted file. Passing null (Nothing in VB) will use the path on the fileName, if any. Passing the empty string ("") will insert the item at the root path within the archive.

Try this:

 $e = $zipfile.AddFile($o.FullName, $o.Name)

It is also possible that this does what you want:

 $e = $zipfile.AddFile($o.FullName, "")

这篇关于如何在powershell中使用DotNetZip仅压缩文件而不是完整路径层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆