C# 创建没有第三方DLL的Zip。获取部件URI必须以正斜杠错误开头
我有一个问题,正因为这个原因,我正在研究一个纯c#的zip文件解决方案。这是一个典型的asp网站,我将在服务器上注册我的dll。我已经测试了其他第三方库 我得到以下错误: 部分URI必须以正斜杠开头 下面是我通过谷歌搜索能够构建的实现。我的错误在第行的方法“AddFileToZip”中: 主要方法:C# 创建没有第三方DLL的Zip。获取部件URI必须以正斜杠错误开头,c#,archive,C#,Archive,我有一个问题,正因为这个原因,我正在研究一个纯c#的zip文件解决方案。这是一个典型的asp网站,我将在服务器上注册我的dll。我已经测试了其他第三方库 我得到以下错误: 部分URI必须以正斜杠开头 下面是我通过谷歌搜索能够构建的实现。我的错误在第行的方法“AddFileToZip”中: 主要方法: public bool ArchiveFile(string fileDir, string fileToArchive, string newArchiveFileName) { File
public bool ArchiveFile(string fileDir, string fileToArchive, string newArchiveFileName)
{
FileSystem fso = new FileSystem();
bool ok = !String.IsNullOrWhiteSpace(fileDir) &&
!String.IsNullOrWhiteSpace(fileToArchive) &&
fso.FileExists(Path.Combine(fileDir, fileToArchive)) &&
fileToArchive.Contains(".");
if (ok)
{
if (!String.IsNullOrWhiteSpace(newArchiveFileName))
{
if (!newArchiveFileName.ToLower().Contains(".zip"))
newArchiveFileName = String.Concat(newArchiveFileName, ".zip");
}
else
{
string filePart = fileToArchive.Substring(0, fileToArchive.LastIndexOf(".", System.StringComparison.Ordinal));
newArchiveFileName = String.Concat(filePart, ".zip");
}
//if archve file already exists then delete it
if (fso.FileExists(Path.Combine(fileDir, newArchiveFileName)))
ok = fso.FileDelete(Path.Combine(fileDir, newArchiveFileName));
}
if (ok)
{
Impersonate impersonate = new Impersonate();
impersonate.DoImpersonate();
Package zipFile = Package.Open(Path.Combine(fileDir, newArchiveFileName), FileMode.OpenOrCreate, FileAccess.ReadWrite);
FileInfo file = new FileInfo(Path.Combine(fileDir, fileToArchive));
AddFileToZip(file, zipFile);
zipFile.Close();
impersonate.Dispose();
ok = fso.FileExists(Path.Combine(fileDir, newArchiveFileName));
}
return ok;
}
protected void AddFileToZip(FileInfo file, Package zipFilePackage)
{
string physicalfilePath = file.FullName;
//Check for file existing. If file does not exists,
//then add in the report to generate at the end of the process.
if (File.Exists(physicalfilePath))
{
string fileName = Path.GetFileName(physicalfilePath);
// Remove the section of the path that has "root defined"
physicalfilePath = physicalfilePath.Replace("./", "");
// remove space from the file name and replace it with "_"
physicalfilePath = physicalfilePath.Replace(fileName, fileName.Replace(" ", "_"));
try
{
//Define URI for this file that needs to be added within the Zip file.
Uri partURI = new Uri(physicalfilePath, UriKind.Relative);
string contentType = GetFileContentType(physicalfilePath);
PackagePart newFilePackagePart = zipFilePackage.CreatePart(partURI, contentType, CompressionOption.Normal);
byte[] fileContent = File.ReadAllBytes(physicalfilePath);
newFilePackagePart.GetStream().Write(fileContent, 0, fileContent.Length);
}
catch (Exception ex)
{
throw new ApplicationException("Unable to archive: " + ex.Message);
}
}
}
protected string GetFileContentType(string path)
{
string contentType = System.Net.Mime.MediaTypeNames.Application.Zip;
switch (Path.GetExtension(path).ToLower())
{
case (".xml"):
{
contentType = System.Net.Mime.MediaTypeNames.Text.Xml;
break;
}
case (".txt"):
{
contentType = System.Net.Mime.MediaTypeNames.Text.Plain;
break;
}
case (".rtf"):
{
contentType = System.Net.Mime.MediaTypeNames.Application.Rtf;
break;
}
case (".gif"):
{
contentType = System.Net.Mime.MediaTypeNames.Image.Gif;
break;
}
case (".jpeg"):
{
contentType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
break;
}
case (".tiff"):
{
contentType = System.Net.Mime.MediaTypeNames.Image.Tiff;
break;
}
case (".pdf"):
{
contentType = System.Net.Mime.MediaTypeNames.Application.Pdf;
break;
}
case (".doc"):
case (".docx"):
case (".ppt"):
case (".xls"):
{
contentType = System.Net.Mime.MediaTypeNames.Text.RichText;
break;
}
}
return contentType;
}
问题在于创建partUri。我已经使用了完整路径,因为它应该是文件名
Uri partUri = PackUriHelper.CreatePartUri(new Uri(String.Concat(@".\", fileName), UriKind.Relative));
这种联系有助于解决这个问题
新Uri(physicalfilePath,UriKind.Relative)返回了什么?提示:使用@的逐字字符串文字更易于阅读:
string dir=@“\\filebox01\data\test”代码>。请不要在标题前加上“C”。这就是标记的用途。进行了以下更改:string dir=@“\\filebox01\data\test\text.xls”;Uri partURI=新Uri(physicalfilePath,UriKind.Relative);当我展开partURI时给出{\\filebox01\data\test\text.xls},对于OriginalString,我得到上面的值,IsAbsoluteUri=false。所有剩余项引发以下异常:System.InvalidOperationException
Uri partUri = PackUriHelper.CreatePartUri(new Uri(String.Concat(@".\", fileName), UriKind.Relative));