Exemplo n.º 1
0
        public void Save()
        {
            List <CompressedFileRecord> objSavedRecords = new List <CompressedFileRecord>();

            string strTempFilePath = Path.GetTempFileName();

            using (FileStream objTempFileStream = new FileStream(strTempFilePath, FileMode.Create, FileAccess.ReadWrite))
            {
                CompressedFileRecord[] objInsertedRecords = CompressedFileRecordManager.InsertedRecords;
                foreach (CompressedFileRecord objInsertedRecord in objInsertedRecords)
                {
                    using (FileStream objInputFile = new FileStream(objInsertedRecord.OriginalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        int intStartingPosition = (int)objTempFileStream.Position;
                        CompressionManager.Compress(objInputFile, objTempFileStream);

                        int intCompressedSize = (int)objTempFileStream.Position - intStartingPosition;
                        CompressedFileRecord objSavedRecord = new CompressedFileRecord(objInsertedRecord.OriginalFilePath, intCompressedSize, objInsertedRecord.RelativePath);
                        objSavedRecord.Keys.AddRange(objInsertedRecord.Keys);
                        objSavedRecords.Add(objSavedRecord);
                    }

                    objTempFileStream.Flush();
                }

                CompressedFileRecord[] objUpdatedRecords = CompressedFileRecordManager.UpdatedRecords;
                foreach (CompressedFileRecord objUpdatedRecord in objUpdatedRecords)
                {
                    int intFileStartIndex = CompressedFileRecordManager.FindFileStartIndex(objUpdatedRecord);
                    if (intFileStartIndex != -1)
                    {
                        CompressedStream.Position = intFileStartIndex;
                        CompressionManager.CopyToStream(CompressedStream, objTempFileStream, objUpdatedRecord.CompressedSize);
                        objSavedRecords.Add(objUpdatedRecord);
                    }

                    objTempFileStream.Flush();
                }

                objTempFileStream.Position = 0;
                CompressedStream.SetLength(0);
                CompressedStream.Position = 0;

                CompressedStream.Write(CompressedStreamSignature, 0, CompressedStreamSignature.Length);

                string strSavedRecords       = CompressedFileRecordManager.ToString(objSavedRecords.ToArray());
                byte[] bytSavedRecords       = ASCIIEncoding.ASCII.GetBytes(strSavedRecords);
                byte[] bytCompressedRecords  = CompressionManager.Compress(bytSavedRecords);
                int    intRecordLength       = bytCompressedRecords.Length;
                byte[] bytHeaderRecordLength = BitConverter.GetBytes(intRecordLength);
                CompressedStream.Write(bytHeaderRecordLength, 0, bytHeaderRecordLength.Length);
                CompressedStream.Write(bytCompressedRecords, 0, bytCompressedRecords.Length);

                CompressionManager.CopyToStream(objTempFileStream, CompressedStream);
                CompressedStream.Flush();
            }

            File.Delete(strTempFilePath);
            Refresh();
        }
Exemplo n.º 2
0
 public static byte[] CompressFromString(string strInputString)
 {
     byte[] bytData           = ASCIIEncoding.ASCII.GetBytes(strInputString);
     byte[] bytCompressedData = CompressionManager.Compress(bytData);
     return(bytCompressedData);
 }