Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to download two or more files in one zip file. But zip file contains only first file and other files are not there. Below is the code :


C#
string[] fileLists = Directory.GetFiles("C:\\Downloadable");
        string strNow = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now);
        using (MemoryStream mstr = new MemoryStream())
        {
            using (ZipOutputStream ZOS = new ZipOutputStream(mstr))
            {
                for (int i = 0; i < fileLists.Length; i++)
                {
                    byte[] content = File.ReadAllBytes(fileLists[i]);
                    string fileName = fileLists[i].Substring(16);
                    ZOS.CompressionLevel = CompressionLevel.BestCompression;
                    ZOS.PutNextEntry(fileName);
                    ZOS.Write(content, 0, content.Length - 1);
                }
                byte[] zipFile = new byte[(int)mstr.Length];
                Response.Clear();
                Array.Copy(mstr.ToArray(), zipFile, (int)mstr.Length);
                Response.AppendHeader("content-disposition", "attachment;filename=" + strNow);
                Response.ContentType = "application/x-zip-compressed";
                Response.BinaryWrite(zipFile);
                Response.Expires = 0;
                Response.End();
            }
        }

}
}

If any idea please update. Thanks.
Posted

snehashis ghosh 2 wrote:
string fileName = fileLists[i].Substring(16);


This looks like code that's making a broad assumption. Are you sure that assumption is always true ?

All of this reads to me as if the third party library you're using is not working. Your best bet IMO is to talk to the authors of that library. Your MemoryStream is not magically constrained to EXACTLY the size of the first file, increasing the size of a memory stream is most certainly not where your issue lies.
 
Share this answer
 
Christian has succinctly stated what the problem seems to be. If you want to get the filename, you can do this using System.IO.Path.GetFileName.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900