我正在使用SharpZipLib创建tar.gz归档文件,但我需要在文件名前加上一个目录名.也就是说,进入归档的文件看起来像foo.exe个,但归档需要知道它们是foo-1.0/foo.exe,所以解压时会创建正确的目录 struct .

输入文件中已有not个名称正确的目录,因此在归档过程中,需要设置名称.

这就是我到目前为止所拥有的:

    var archiveName = $"bin/{projectVersion}.tar.gz";
    using var archiveStream = File.Create(archiveName);
    using var gzipStream = new GZipOutputStream(archiveStream);
    using var archive = TarArchive.CreateOutputTarArchive(gzipStream);
    archive.RootPath = projectVersion;
    foreach (var path in Directory.GetFileSystemEntries(publishPath)) {
        var entry = TarEntry.CreateEntryFromFile(path);
        entry.Name = $"{projectVersion}/{Path.GetFileName(path)}";
        archive.WriteEntry(entry, true);
    }

其中projectVersion=例如"foo-1.0".

我希望设置archive.RootPath或将每个entry.Name设置为适当的前缀字符串都能完成这项工作,但即使我同时执行这两项操作,当我解压生成的归档文件时,文件只是被转储到当前目录中.

我错过了什么?

推荐答案

You might need to modify the way you set the entry.Name, i.e. setting the entry.Name with the prefixed directory.
The key would be to make sure the full path, including the directory name, is correctly assigned to each entry in the archive.

var archiveName = $"bin/{projectVersion}.tar.gz";
using var archiveStream = File.Create(archiveName);
using var gzipStream = new GZipOutputStream(archiveStream);
using var archive = TarArchive.CreateOutputTarArchive(gzipStream);

foreach (var filePath in Directory.GetFiles(publishPath)) {
    var entry = TarEntry.CreateEntryFromFile(filePath);
    // Make sure the directory structure is preserved in the entry's name
    entry.Name = $"{projectVersion}/{Path.GetFileName(filePath)}";
    archive.WriteEntry(entry, true);
}

That now takes each file in publishPath, prefixes it with the projectVersion directory, and sets the entry.Name.
That makes sure upon extraction, the directory structure foo-1.0/ will be created, and files will be placed inside it.

此外,Directory.GetFileSystemEntries(publishPath)被更改为Directory.GetFiles(publishPath),以确保只处理文件,而不是目录.这应该可以避免目录条目的潜在问题.

Csharp相关问答推荐

Rx.Net -当关闭序列被触发时如何聚合消息并发出中间输出?

List T.AddRange在传递ConcurrentDictionary作为参数时引发ArgumentExcellent

我可以 suppress 规则CS 9035一次吗?

EF Core判断是否应用了AsSplitQuery()

"virtual"修饰符对接口成员有什么影响?

AsNoTrackingWithIdentitySolutions()似乎不起作用?

最新的Mediatr和具有同步方法的处理程序Handle:并非所有代码路径都返回值"

Thad.Sept()vs Task.Delay().Wait()

如何从ASP.NET核心MVC视图和Blazor传递数据

如何在毛伊岛应用程序中完美地同步视图模型和视图的加载?

如何将DotNet Watch与发布配置和传递给应用程序的参数一起使用?

在C#中,非静态接口方法的抽象和虚拟是冗余的吗?

如何避免在.NET中将日志(log)写入相对路径

JSON串行化程序问题.SQLite中的空值

Visual Studio如何使用当前的框架?

具有嵌套属性的IGGroup

C#中使用ReadOnlySpan的泛型类型推理

如何从原始图像到新创建的图像获得相同的特定 colored颜色 ,并且具有相同的 colored颜色 量和相同的宽度和高度?

在使用.NET EF Core DbContext属性之前,是否应使用null判断

在C#中删除多个不同名称的会话