-1

I have an upload button in which, when I upload file my file, must be uploaded in zip or compress form in specified path on server

I tried ===>

        string strFileName = string.Empty;
        string strserverPath = string.Empty;
        if (UploadFiles.HasFile)
        {
            string abcPATH = tvFolders.SelectedValue.ToString();
            string rootPath = tvFolders.SelectedNode.ToString();
            string fname = Path.GetFileName(UploadFiles.PostedFile.FileName);
            try
            {
                strserverPath = abcPATH + "\\" + fname;

                //string strName = Path.GetFileName(UploadFiles.PostedFile.FileName);

                Stream myStream = UploadFiles.PostedFile.InputStream;
                byte[] myBuffer = new byte[myStream.Length + 1];
                myStream.Read(myBuffer, 0, myBuffer.Length);
                myStream.Close();
                FileStream myCompressedFile = default(FileStream);
                myCompressedFile = File.Create(Server.MapPath(Path.ChangeExtension("~/"+strserverPath, "zip")));
                GZipStream myStreamZip = new GZipStream(myCompressedFile, CompressionMode.Compress);
                myStreamZip.Write(myBuffer, 0, myBuffer.Length);
                myStreamZip.Close();
                //asp.net c#
                //UploadFiles.PostedFile.SaveAs(Server.MapPath("~/" + strserverPath));
                listofuploadedfiles.Text = "done";
            }
            catch (Exception ex)
            {
                Response.Write("Error" + ex.Message);
            }
        }
        else
            listofuploadedfiles.Text = "not done";    
    }
Peter G.
  • 14,786
  • 7
  • 57
  • 75

3 Answers3

1

In .net 4.5 there is a new System.IO.Compression.ZipFile namespace which has a friendly api to create zip files than using the tricky to use .net 3.0 package approach.

The only caveat is that it works with folder structures rather than files directly.

As the commenter below suggests, this isn't a 'get you started' to solve you exact problem. But perhaps a new approach that could be easier from the outset.

using System;
using System.IO;
using System.IO.Compression;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string startPath = @"c:\example\start";
            string zipPath = @"c:\example\result.zip";
            string extractPath = @"c:\example\extract";

            ZipFile.CreateFromDirectory(startPath, zipPath);

            ZipFile.ExtractToDirectory(zipPath, extractPath);
        }
    }
}

Taken from http://msdn.microsoft.com/en-us/library/vstudio/system.io.compression.zipfile

It should get you started in perhaps using the zipfile approach instead.

Alex KeySmith
  • 16,657
  • 11
  • 74
  • 152
  • OP is showing code that is not working, not asking to get started. Please ask for clarification using a comment instead of posting a generic, duplicate answer, or close-vote as duplicate accordingly. – CodeCaster May 13 '14 at 10:24
  • Good point @CodeCaster I've added some details to it being a new approach. – Alex KeySmith May 13 '14 at 11:56
  • sir i want dynamic path, and my file is not zip file actually i want when i uploading my file for exp. i have xyz.pdf file now when i upload to the server my file must be upload in zip/rar file like xyz.zip/rar – user3622698 May 14 '14 at 04:53
  • @user3622698 you could perhaps create a folder first http://msdn.microsoft.com/en-us/library/54a0at6s(v=vs.110).aspx for each file uploaded? – Alex KeySmith May 14 '14 at 08:12
0

.NET below the version 4.5 does not natively support the ZIP file format.

You can look into libraries which add that support, such as SharpZipLib

DrCopyPaste
  • 4,023
  • 1
  • 22
  • 57
MarkO
  • 2,143
  • 12
  • 14
  • As of .net 4.5 there is support: http://msdn.microsoft.com/en-us/library/vstudio/system.io.compression.zipfile – Alex KeySmith May 13 '14 at 09:42
  • 1
    @Alex tnx, learned something new today :) – MarkO May 13 '14 at 09:52
  • No worries, it's surprising how long it's taken for it to be baked in. There even used to be a little known about ZipPackage class http://msdn.microsoft.com/en-us/library/system.io.packaging.zippackage(v=vs.85).aspx but it's a bit weird to work with. And would of been better to use things like SharpZipLib. But the new .net 4.5 take on it looks nice. – Alex KeySmith May 13 '14 at 10:19
0
using System;
using System.IO;
using System.IO.Packaging;

namespace ZipSample
{
class Program
{
    static void Main(string[] args)
    {
        AddFileToZip("Output.zip", @"C:\Windows\Notepad.exe");
        AddFileToZip("Output.zip", @"C:\Windows\System32\Calc.exe");
    }

    private const long BUFFER_SIZE = 4096;

    private static void AddFileToZip(string zipFilename, string fileToAdd)
    {
        using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
        {
            string destFilename = ".\\" + Path.GetFileName(fileToAdd);
            Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
            if (zip.PartExists(uri))
            {
                zip.DeletePart(uri);
            }
            PackagePart part = zip.CreatePart(uri, "",CompressionOption.Normal);
            using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
            {
                using (Stream dest = part.GetStream())
                {
                    CopyStream(fileStream, dest);
                }
            }
        }
    }

    private static void CopyStream(System.IO.FileStream inputStream, System.IO.Stream outputStream)
    {
        long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE;
        byte[] buffer = new byte[bufferSize];
        int bytesRead = 0;
        long bytesWritten = 0;
        while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            outputStream.Write(buffer, 0, bytesRead);
            bytesWritten += bufferSize;
        }
    }
}
}

Coding has been Refer via these websites

1.Creating Zip archives in .NET (without an external library like SharpZipLib) 2.http://madprops.org/blog/zip-your-streams-with-system-io-packaging/
3.Create normal zip file programmatically

Additional Reference :

You can Refer to these sites.It may be useful to you.

http://forums.asp.net/t/1086292.aspx?How+to+create+zip+file+using+C+

Create normal zip file programmatically

http://www.dotnetperls.com/zipfile

http://www.aspdotnet-suresh.com/2013/04/create-zip-files-in-aspnet-c-vbnet-zip.html

http://www.codeproject.com/Questions/450505/create-zip-file-in-asp-net

Good Luck..

Community
  • 1
  • 1
coolprarun
  • 1,153
  • 2
  • 15
  • 22
  • i seen u r last link . in that what is use of "using Ionic.Zip" and how i can use this file bcoz i was tried u r link code before to ask qsion ,and i fase problem of the that .dll file so plz help.me about that Ionic.Zip.dll file. – user3622698 May 13 '14 at 09:45