Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7577485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:11:49+00:00 2026-05-30T17:11:49+00:00

I have a problem and due to that reason i am working on a

  • 0

I have a problem and due to that reason i am working on a pure c# solution to zip files. This is for a classic asp site and I will register my dll on ther server. I already have tested other third party libraries…

I am getting the following error:
Part URI must start with a forward slash.

Here is the implementation that i have been able to build by googling around. My error is in method “AddFileToZip” on line:

PackagePart newFilePackagePart = zipFilePackage.CreatePart(partURI, contentType, CompressionOption.Normal);

[TestMethod]
public void ArchiveFile()
{
    string dir = "\\\\filebox01\\data\\test";
    string file = "text.xls";

    ZipClassic zip = new ZipClassic();

    bool ok = zip.ArchiveFile(dir, file, "singleFileArchive.zip");
    Assert.IsTrue(ok);
}

Main method:

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:11:51+00:00Added an answer on May 30, 2026 at 5:11 pm

    Problem was in creating partUri. I have used the full path where as it should have been the file name.

    Uri partUri = PackUriHelper.CreatePartUri(new Uri(String.Concat(@".\", fileName), UriKind.Relative));
    

    This link helped to reolve the problem.

    http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The whole internet is full of such problem: due to some reason web site
I have this problem I've been sitting with. My tableView that I put inside
I have a ridiculous question due to a ridiculous problem. Normally if I want
I have been having the following problem, i think it's probably due to the
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have tried a few things to solve this problem but I can't seem
I have been struggling with this seeminly easy problem for 48 hours, and I
I am working on a Java application that will use some Hibernate (annotated by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.