0

I need to create zip file from the folder, path:

D:\Nagaraj\New Project Read Document\TCBILPOS\TCBILPOS\TCBILPOS\FileBuild\HOST

within that host folder there are 7 txt files.

I want to create zip file HOST.zip in the folder above:

D:\Nagaraj\New Project Read Document\TCBILPOS\TCBILPOS\TCBILPOS\FileBuild
0

2 Answers 2

1

I've used Ionic ZIP for this in our own projects.

using (ZipFile zip = new ZipFile())
 {
     // add this map file into the "images" directory in the zip archive
     zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
     // add the report into a different directory in the archive
     zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
     zip.AddFile("ReadMe.txt");
     zip.Save("MyZipFile.zip");
 }
Sign up to request clarification or add additional context in comments.

Comments

0
public class Ziper
{
    public static string MapPathReverse(string fullServerPath)
    {
        return @"~\" + fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath, String.Empty);
    }
    public static void Zip(HttpResponse Response, HttpServerUtility Server, string[] pathes)
    {
        Response.Clear();
        Response.BufferOutput = false; // false = stream immediately
        System.Web.HttpContext c = System.Web.HttpContext.Current;
        //String ReadmeText = String.Format("README.TXT\n\nHello!\n\n" +
        //                                 "This is text for a readme.");
        string archiveName = String.Format("archive-{0}.zip",
                                          DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
        Response.ContentType = "application/zip";
        Response.AddHeader("content-disposition", "filename=" + archiveName);

        var path = Server.MapPath(@"../Images/TempFile/TempFile" + DateTime.Now.Ticks);
        if (Directory.Exists(path) == false)
            Directory.CreateDirectory(path);
        var pathzipfile = Server.MapPath(@"../Images/TempFile/zip_" + DateTime.Now.Ticks + ".zip");
        for (int i = 0; i < pathes.Length; i++)
        {
            if (File.Exists(pathes[i]))
            {
                string dst = Path.Combine(path, Path.GetFileName(pathes[i]));
                File.Copy(pathes[i], dst);
            }

        }
        if (File.Exists(pathzipfile))
            File.Delete(pathzipfile);
        ZipFile.CreateFromDirectory(path, pathzipfile);
        {
            byte[] bytes = File.ReadAllBytes(pathzipfile);
            Response.OutputStream.Write(bytes, 0, bytes.Length);
        }
        Response.Close();
        File.Delete(pathzipfile);
        Directory.Delete(path, true);
    }
    public Ziper()
    {

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.