我是编程新手,所以请耐心等待。
我目前正在使用 Visual C# 2010 Express,.NET Framework 4.0 开发一个小程序,它在 Linux 机器上启动一个脚本(创建文件 /tmp/logs.tgz),下载文件然后我想提取它。运行脚本并通过 Renci.SshNet 下载文件完美无缺。但是当我想提取它时,它给了我一个错误“NotSupportedException”我的文件路径格式不正确(我认为不是这种情况?)。我直接从这里复制并粘贴了代码(从 TGZ (.tar.gz) 中简单完整提取)并根据我的需要对其进行了编辑:
using System.IO;
using System.IO.Compression;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
//it executed the Script and created the file on the Linux Machine /tmp/logs.tgz
//now I want to download it
string myTime = DateTime.Now.ToString("yyyyMMdd");
var pathWithEnv = (@"%USERPROFILE%\Desktop\logs" + myTime + ".tgz");
var filePath = Environment.ExpandEnvironmentVariables(pathWithEnv);
string localFile = filePath;
//then downloads /tmp/logs.tgz to ..\Desktop\logs+ myTime +.tgz
//everything great until now. here I want to extract .TGZ:
var pathWithEnv2 = (@"%USERPROFILE%\Desktop\logs" + myTime);
var fileDir = Environment.ExpandEnvironmentVariables(pathWithEnv2);
string localDir = fileDir;
Stream inStream = File.OpenRead(localFile);
Stream gzipStream = new GZipInputStream(inStream);
TarArchive tarArchive = TarArchive.CreateInputTarArchive(gzipStream);
//ERROR OCCURS HERE:
tarArchive.ExtractContents(localDir);
tarArchive.Close();
gzipStream.Close();
inStream.Close();
我什至尝试在没有 EnviromentVariable 的情况下设置 localFile 和 localDir 字符串,但这没有帮助。我试过: - 直接在 C:\(或映射的网络驱动器 U:)上下载并解压缩它,以防止文件名过长(不应该这样,因为它永远不会超过 86 个字符)。- string = @"C:..\logs", string = "C:\..\logs", string = @"C:..\logs\" 等 - 没有 myTime 尝试过 - 使用 ICSharpCode.SharpZipLib 。核;
我做了一个 MessageBox.Show(localDir); 在 tarArchive.ExtractContents(localDir) 之前;它显示“C:\Users\Baumann\Desktop\logs20140530”是正确的,这就是我想将其提取到的目录。即使在执行之前创建目录也无济于事。
我还创建了一个只有一个按钮的新项目,该按钮应该启动提取并发生相同的错误。
我试过,分开做,先提取 GZip,然后提取 .tar,但在提取 GZip 时它也给了我同样的错误(使用 ICSharpCode.SharpZipLib.Core;当然)。
让我更加疯狂的是,它开始提取它,但不是所有的,在它失败之前。并且总是相同的文件,我不清楚为什么这些文件,为什么不是其他文件。
我在 Windows 8.1 上,使用 SharpZipLib 0.86.0.518,直接从网站下载。
提前致谢。