Wednesday, 3 February 2016

Read Zip file using C#

 // read JSON directly from a file
            string file = @"C:\Users\user\Documents\Visual Studio 2015\Projects\PushNotification_Service\PushNotification_Service\package.json";
            List<Class1> lst = new List<Class1>();
            using (StreamReader r = new StreamReader(file)) {
                string json = r.ReadToEnd();
                lst = JsonConvert.DeserializeObject<List<Class1>>(json);

                //add and update json in file
                //Class1 class34 = lst.Where(x => x.WareHouseId == "12").FirstOrDefault();
                //class34.Category = "Updated Vegetable";
                //class34.Status = "false";

                //delete object from json file
                lst.RemoveAll(x => x.WareHouseId == "1");
            }
            string output = Newtonsoft.Json.JsonConvert.SerializeObject(lst, Newtonsoft.Json.Formatting.Indented);
            File.WriteAllText(@"C:\Users\user\Documents\Visual Studio 2015\Projects\PushNotification_Service\PushNotification_Service\package.json", output);
            string zipToUnpack = @"c:\users\user\documents\visual studio 2015\Projects\PushNotification_Service\PushNotification_Service\PowerMerge_1_0_5_2013_managed.zip";
            string unpackDirectory = "customizations.xml";
            using (ZipFile zip = ZipFile.Read(zipToUnpack))
            {
                MemoryStream data = new MemoryStream();
                ZipEntry entry = zip[unpackDirectory];
                entry.Extract(data);
                data.Seek(0, SeekOrigin.Begin);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(data);
                using (var stream = entry.OpenReader())
                {
                    var buffer = new byte[2048];
                    int n;
                    while ((n = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        // do something with the buffer here.
                    }
                }
            }

            using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
            {

                // here, we extract every entry, but we could extract conditionally
                // based on entry name, size, date, checkbox status, etc. 
                foreach (ZipEntry e in zip1)
                {
                    if (e.FileName == unpackDirectory)
                    {

                    }
                    //e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
                }
            }

No comments:

Post a Comment