How to zip only files and not the full path hierarchy with DotNetZip in powershell?

泄露秘密 提交于 2019-11-29 03:19:38

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, "")
mjolinor

Not tested, but I think this should work.

[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\"
    set-location $directory
    $children = get-childitem *.log
    foreach ($o in $children)
    {
        $e = $zipfile.AddFile($o.Name)
       }
    }
    $zipfile.Save()
    $zipfile.Dispose()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!