آموزش زبان های برنامه نویسی

آموزش انواع زبان های برنامه نویسی

آموزش زبان های برنامه نویسی

آموزش انواع زبان های برنامه نویسی

نحوه ساخت فایل زیپ در #C


نحوه ساخت فایل زیپ در #C


در این مثال ما یک فایل تکستی داریم که آن را به فایل JSON تبدیل کرده ایم و بعد یه مجموعه عکس را از دیتابیس که فایل های آن بصورت باینری است را خوانده ایم و آن را به یک فایل زیپ تبدیل کرده ایم.و نهایتن آن را در مرورگر قابل دانلود کرده ایم

و همچنین امکان ذخیره آن نیز وجود دارد.


        private void CreateZipFiles(string alljson)

        {

          


            int RegionId = Convert.ToInt32(drpRegionId.SelectedValue);

            byte[] jsondata = GetBytes(alljson);


            string jsonName = string.Format(RegionId.ToString() + ".json");

            string zipName = string.Format(RegionId.ToString() + ".zip");


            using (DAL.DataClassesDataContext Context = new DAL.DataClassesDataContext())

            {

                using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())

                {

                    zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Level5;

                    var ItemAtachment = Context.ItemAttachment_FCH(null, null, RegionId).ToList();

                    int countid = 1;

                    int tempItemId = 0;

                    foreach (var item in ItemAtachment)

                    {

                        if (item.ItemAttachmentTypeid == 1)

                        {

                            try

                            {

                                //صاحب پروانه

                                Byte[] ItemImage = (Byte[])item.ItemAttachmentContent.ToArray();

                                jsonName = item.ItemId + "-" + "main" + ".jpg";

                                zip.AddEntry(jsonName, ItemImage);

                            }

                            catch

                            {

                            }

                        }

                        else if (item.ItemAttachmentTypeid == 2)

                        {

                            //سر در مشاور املاک

                            if (tempItemId != item.ItemId)

                            {

                                countid = 1;

                            }


                            Byte[] ItemImage = (Byte[])item.ItemAttachmentContent.ToArray();

                            jsonName = item.ItemId + "-" + "sub" + "-" + countid + ".jpg";

                            zip.AddEntry(jsonName, ItemImage);

                            tempItemId = item.ItemId;

                            countid++;

                        }


                    }


                    jsonName = string.Format(RegionId.ToString() + ".json");

                    zip.AddEntry(jsonName, jsondata);

                    //zip.Save(Server.MapPath(zipName));


                    Response.Clear();

                    Response.BufferOutput = false;


                    Response.ContentType = "application/zip";

                    Response.AddHeader("content-disposition", "attachment; filename=" + zipName);

                    zip.Save(Response.OutputStream);

                    Response.End();


                }

            }


        }

// تبدیل فایل متنی به بایت

static byte[] GetBytes(string str)

        {

            byte[] bytes = new byte[str.Length * sizeof(char)];

            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);

            return bytes;

        }