Esempio n. 1
0
        private static bool CreateCheckSumFile(LiveUpdateCollectionEntity entity, string path)
        {
            bool result;

            using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                try
                {
                    DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(LiveUpdateCollectionEntity));
                    dataContractSerializer.WriteObject(fileStream, entity);
                    result = true;
                }
                catch
                {
                    result = false;
                }
                finally
                {
                    fileStream.Close();
                }
            }
            return(result);
        }
Esempio n. 2
0
        public static void ZipFiles(string inputFolderPath, string outputFolderPath, string outputFileName, string password, bool isCreated)
        {
            LiveUpdateCollectionEntity liveUpdateCollectionEntity = new LiveUpdateCollectionEntity();
            List <string> list = FileHelper.GenerateFileList(inputFolderPath);
            int           num  = Directory.GetParent(inputFolderPath).ToString().Length;

            num++;
            FileStream      fileStream      = null;
            ZipOutputStream zipOutputStream = null;
            string          path            = Path.Combine(outputFolderPath, outputFileName);
            string          directoryName   = Path.GetDirectoryName(path);

            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
            if (!isCreated && File.Exists(path))
            {
                return;
            }
            try
            {
                zipOutputStream = new ZipOutputStream(File.Create(path));
                if (!string.IsNullOrEmpty(password))
                {
                    zipOutputStream.Password = password;
                }
                zipOutputStream.SetLevel(9);
                foreach (string text in list)
                {
                    ZipEntry zipEntry = new ZipEntry(text.Remove(0, num));
                    zipOutputStream.PutNextEntry(zipEntry);
                    if (!text.EndsWith("/"))
                    {
                        fileStream = File.OpenRead(text);
                        byte[] array = new byte[fileStream.Length];
                        fileStream.Read(array, 0, array.Length);
                        zipOutputStream.Write(array, 0, array.Length);
                        liveUpdateCollectionEntity.Data.Add(new LiveUpdateEntity(text, BitConverter.ToString(FileHelper.md5Provider.ComputeHash(fileStream))));
                    }
                }
                string text2 = Path.Combine(inputFolderPath, "checksum.xml");
                if (FileHelper.CreateCheckSumFile(liveUpdateCollectionEntity, text2))
                {
                    ZipEntry zipEntry = new ZipEntry("checksum.xml");
                    zipOutputStream.PutNextEntry(zipEntry);
                    fileStream = File.OpenRead(text2);
                    byte[] array = new byte[fileStream.Length];
                    fileStream.Read(array, 0, array.Length);
                    zipOutputStream.Write(array, 0, array.Length);
                    string path2 = Path.Combine(directoryName, "checksum.xml");
                    if (File.Exists(path2))
                    {
                        File.Delete(path2);
                    }
                    File.Copy(text2, Path.Combine(directoryName, "checksum.xml"));
                }
                zipOutputStream.Finish();
                zipOutputStream.Close();
            }
            catch (Exception value)
            {
                Console.WriteLine(value);
            }
            finally
            {
                if (zipOutputStream != null)
                {
                    zipOutputStream.Dispose();
                }
                if (fileStream != null)
                {
                    fileStream.Dispose();
                }
            }
        }