Help with System.Uri, Part URI must start with a forward slash

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am creating a com component that will be used by a classic asp site. Intent is to zip files and folders.

Box: windows 2008 6bit
Environment: 4.0 VS 2010
<span class="pun I am getting following error:
Part URI must start with a forward slash
On the following line:
<pre class="prettyprint PackagePart newFilePackagePart = zipFilePackage.CreatePart(partURI, contentType, CompressionOption.Normal);[/code]
I am passing to it the path of the file that i want to zip as:
<pre class="prettyprint string dir = @"\filebox01datatest";
string file = "test.xls";[/code]
And partUri in the problem code is:
<pre class="prettyprint Uri partURI = new Uri(physicalfilePath, UriKind.Relative);[/code]
This gives:
{\filebox01datatesttext.xls}
When i expand partURI, for OriginalString i get the above value and IsAbsoluteUri=false. All remaining items throw the following exception: System.InvalidOperationException
Full code is below...
Test Method:
<pre class="prettyprint [TestMethod]
public void ArchiveFile()
{
string dir = @"\filebox01datatest";
string file = "test.xls";
ZipClassic zip = new ZipClassic();
bool ok = zip.ArchiveFile(dir, file, "singleFileArchive.zip");
Assert.IsTrue(ok);
}[/code]
And here is the Archivefile method:
<pre class="prettyprint 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.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;
}[/code]
<br/>
I am going to post the link to the tutorial shortly that i am following.

View the full article
 
Back
Top