Sharpcompress:上下文中不存在关键字
Sharpcompress: keywords don't exist in context
我正在尝试从我的 C# 项目中提取一个 .tgz 文件,我尝试按照以下问题的答案进行操作:
Decompress tar files using C#
但是,此代码示例对我不起作用:
using (Stream stream = File.OpenRead(tarPath))
{
var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(extractPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
}
}
}
我得到两个错误:
1) 提取选项 "does not exist in the current context"。我成功地使用了 System.IO 和 SharpCompress.Readers,但我找不到 ExtractOptions 所在的位置。
2) 文件 "is a method, which is not valid in the given context"。我不知道为什么会这样!
如果有帮助,我可以使用简单的方法从同一路径成功提取 .zip 文件:
System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);
如果有更好的方法来提取 .tgz 文件,那也会有所帮助!
谢谢
经过一个美好的夜晚,我终于能够让它工作了。
问题 1,“ExtractOptions 不存在”。
可以看到here,ExtractOptions其实是一个enum,需要初始化。这在我使用的示例中没有显示(也许它在早期版本中的工作方式不同?也许示例中使用的命名约定让我失望了?)
问题 2,“文件在给定上下文中无效”
其实我没有提供足够的信息来回答这个问题!问题是 Visual Studio 被认为是理所当然的我试图使用 Controller.File 而不是 System.IO.File (但抱怨第一个在上下文中无效,而不是关于可能的冲突) . Controller.File 用于 Web 应用程序框架 ASP.NET MVC(这是我正在研究的)来创建 FileContentResult 对象。
固定码
(使用 System.IO;使用 SharpCompress.Readers;)
using (Stream stream = System.IO.File.OpenRead(tarPath))
{
var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(extractPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
}
}
}
我正在尝试从我的 C# 项目中提取一个 .tgz 文件,我尝试按照以下问题的答案进行操作:
Decompress tar files using C#
但是,此代码示例对我不起作用:
using (Stream stream = File.OpenRead(tarPath))
{
var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(extractPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite);
}
}
}
我得到两个错误:
1) 提取选项 "does not exist in the current context"。我成功地使用了 System.IO 和 SharpCompress.Readers,但我找不到 ExtractOptions 所在的位置。
2) 文件 "is a method, which is not valid in the given context"。我不知道为什么会这样!
如果有帮助,我可以使用简单的方法从同一路径成功提取 .zip 文件:
System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);
如果有更好的方法来提取 .tgz 文件,那也会有所帮助!
谢谢
经过一个美好的夜晚,我终于能够让它工作了。
问题 1,“ExtractOptions 不存在”。
可以看到here,ExtractOptions其实是一个enum,需要初始化。这在我使用的示例中没有显示(也许它在早期版本中的工作方式不同?也许示例中使用的命名约定让我失望了?)
问题 2,“文件在给定上下文中无效”
其实我没有提供足够的信息来回答这个问题!问题是 Visual Studio 被认为是理所当然的我试图使用 Controller.File 而不是 System.IO.File (但抱怨第一个在上下文中无效,而不是关于可能的冲突) . Controller.File 用于 Web 应用程序框架 ASP.NET MVC(这是我正在研究的)来创建 FileContentResult 对象。
固定码
(使用 System.IO;使用 SharpCompress.Readers;)
using (Stream stream = System.IO.File.OpenRead(tarPath))
{
var reader = ReaderFactory.Open(stream);
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
reader.WriteEntryToDirectory(extractPath, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true });
}
}
}