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 7644219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:37:57+00:00 2026-05-31T09:37:57+00:00

I am using ZipOutputStream from SharpZipLib and I wish to upload the zipped contents

  • 0

I am using ZipOutputStream from SharpZipLib and I wish to upload the zipped contents it creates directly to my MVC post action. I am successfully getting it to post however the parameter of my action method has null as the posted data when it gets to my MVC action.

Here is my Test code I’m using to test this out:

    public void UploadController_CanUploadTest()
    {
        string xml = "<test>xml test</test>"
        string url = "http://localhost:49316/Api/DataUpload/Upload/";

        WebClient client = new WebClient();

        var cc= new CredentialCache();
        cc.Add(new Uri(url),
              "Basic", 
              new NetworkCredential("Testuser", "user"));

        client.Credentials = cc;

        string _UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
        client.Headers.Add(HttpRequestHeader.UserAgent, _UserAgent);
        client.Headers["Content-type"] = "application/x-www-form-urlencoded";

        using (var stream = client.OpenWrite(url, "POST"))
        {
            Zipped zip = new Zipped(stream, Encoding.UTF8, false);

            FileContent content = new FileContent("Upload", xml);

            var uploads = new List<FileContent>();
            uploads.Add(content);

            zip.Compress(uploads);

            stream.Flush();
            stream.Close();
        }
    }

This is my zipped class wrapper:

    public class Zipped : ICompression, IDisposable
{
    private Stream _stream = null;
    private bool _closeStreamOnDispose = true;
    private Encoding _encoding;

    public Zipped()
        : this(new MemoryStream())
    {

    }

    public Zipped(Stream stream)
        : this(stream, Encoding.UTF8, true)
    {
    }

    public Zipped(Stream stream, Encoding encoding)
        : this(stream, encoding, true)
    {
    }

    public Zipped(Stream stream, Encoding encoding, bool closeStreamOnDispose)
    {
        _stream = stream;
        _closeStreamOnDispose = closeStreamOnDispose;
        _encoding = encoding;
    }

    public Stream Compress(IList<FileContent> dataList)
    {
        ZipOutputStream outputStream = new ZipOutputStream(_stream);
        outputStream.SetLevel(9);

        foreach (var data in dataList)
        {
            ZipEntry entry = new ZipEntry(data.Name);
            entry.CompressionMethod = CompressionMethod.Deflated;

            outputStream.PutNextEntry(entry);

            byte[] dataAsByteArray = _encoding.GetBytes(data.Content);

            outputStream.Write(dataAsByteArray, 0, dataAsByteArray.Length);
            outputStream.CloseEntry();
        }

        outputStream.IsStreamOwner = false;
        outputStream.Flush();
        outputStream.Close();

        return _stream;
    }

    public List<FileContent> DeCompress()
    {
        ZipInputStream inputStream = new ZipInputStream(_stream);
        ZipEntry entry = inputStream.GetNextEntry();

        List<FileContent> dataList = new List<FileContent>();

        while(entry != null)
        {
            string entryFileName = entry.Name;

            byte[] buffer = new byte[4096];     // 4K is optimum

            // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size
            // of the file, but does not waste memory.
            // The "using" will close the stream even if an exception occurs.                
            using (MemoryStream tempMemoryStream = new MemoryStream())
            {
                StreamUtils.Copy(inputStream, tempMemoryStream, buffer);

                string copied = _encoding.GetString(tempMemoryStream.ToArray());
                dataList.Add(new FileContent(entry.Name, copied));
            }

            entry = inputStream.GetNextEntry();
        }

        return dataList;

    }

    public void Dispose()
    {
        if(_closeStreamOnDispose)
            _stream.Dispose();
    }

Here is my simple MVC action:

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase uploaded)
    {
        // uploaded is null at this point
    }
  • 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-31T09:37:58+00:00Added an answer on May 31, 2026 at 9:37 am

    If you want to use HttpPostedFileBase in your controller action you need to send a multipart/form-data request from the client and not application/x-www-form-urlencoded.

    In fact you set the content type to application/x-www-form-urlencoded but you are not respecting this because you are directly writing the raw bytes to the request which is invalid. Well, in fact it’s not respecting the HTTP protocol standard but it could still work if you read the raw request stream from the controller instead of using HttpPostedFileBase. I wouldn’t recommend you going that route.

    So the correct HTTP request that you are sending must look like this:

    Content-Type: multipart/form-data; boundary=AaB03x
    
    --AaB03x
    Content-Disposition: form-data; name="uploaded"; filename="input.zip"
    Content-Type: application/zip
    
    ... byte contents of the zip ...
    --AaB03x--
    

    The boundary must be chosen so that it doesn’t appear anywhere in the contents of the file.

    I have blogged about an example of how you could upload multiple files.

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

Sidebar

Related Questions

Using the http://www.ifans.com/forums/showthread.php?t=132024 post from another question i am allowing the user to enter
I'm using the java.util.zip library and ZipOutputStream in order to create a zip file
I have a Java stored procedure which fetches record from the table using Resultset
I would like to upload a directory from an EMR local file system to
I create zip file using ZipOutputStream. I put in the zip one file(both file
Using Android 2.1+. I have a service that gets killed from time to time
Using Rails 3.1. I have some pages that render same layout, except the contents
I am trying to write the data using the pipe input streams. But from
I'm using rubyzip library for zipping files. But I encounter problems. I try: Zip::ZipOutputStream.open('c:/sites/efiling2/test.zip')
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain

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.