Exemple #1
0
        public void ExtractFile(CompressedFileRecord objRecord, Stream objOutputStream)
        {
            if (objRecord == null)
            {
                throw new ArgumentNullException("objRecord", "A valid non-null CompressedFileRecord is required.");
            }
            if (objOutputStream == null)
            {
                throw new ArgumentNullException("objOutputStream", "A valid non-null Stream is required.");
            }

            if (CompressedFileRecordManager.Exists(objRecord) == false)
            {
                throw new Exception("A record for '" + objRecord.RelativePath + "' could not be found.");
            }

            CompressedFileRecordState enuRecordState = CompressedFileRecordManager.GetRecordState(objRecord);

            if (enuRecordState == CompressedFileRecordState.Deleted)
            {
                throw new Exception("A record for '" + objRecord.RelativePath + "' could not be found.");
            }

            ExtractFile(objRecord, enuRecordState, objOutputStream);
        }
Exemple #2
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();
        }
Exemple #3
0
        public byte[] ExtractFile(CompressedFileRecord objRecord)
        {
            if (objRecord == null)
            {
                throw new ArgumentNullException("objRecord", "A valid non-null CompressedFileRecord is required.");
            }

            if (CompressedFileRecordManager.Exists(objRecord) == false)
            {
                throw new Exception("A record for '" + objRecord.RelativePath + "' could not be found.");
            }

            CompressedFileRecordState enuRecordState = CompressedFileRecordManager.GetRecordState(objRecord);

            if (enuRecordState == CompressedFileRecordState.Deleted)
            {
                throw new Exception("A record for '" + objRecord.RelativePath + "' could not be found.");
            }

            using (MemoryStream objMemoryStream = new MemoryStream())
            {
                ExtractFile(objRecord, enuRecordState, objMemoryStream);
                objMemoryStream.Position = 0;

                return(objMemoryStream.ToArray());
            }
        }
Exemple #4
0
        private void Refresh()
        {
            int intFileDataStartingIndex = 0;

            CompressedFileRecord[] objRecords = new CompressedFileRecord[] { };

            CompressedStream.Position = 0;
            if (CompressedStream.Length > 0)
            {
                if (CompressedStream.Length < CompressedStreamSignature.Length)
                {
                    throw new Exception("The stream does not represent a valid format.");
                }

                byte[] bytBuffer = new byte[CompressedStreamSignature.Length];
                CompressedStream.Read(bytBuffer, 0, bytBuffer.Length);

                for (int intIndex = 0; intIndex < bytBuffer.Length; intIndex++)
                {
                    if (bytBuffer[intIndex] != CompressedStreamSignature[intIndex])
                    {
                        throw new Exception("The stream does not represent a valid format.");
                    }
                }

                bytBuffer = new byte[4];
                CompressedStream.Read(bytBuffer, 0, 4);

                int intRecordLength = BitConverter.ToInt32(bytBuffer, 0);
                bytBuffer = new byte[intRecordLength];

                CompressedStream.Read(bytBuffer, 0, intRecordLength);
                intFileDataStartingIndex = (int)CompressedStream.Position;

                byte[] bytRecordBytes = CompressionManager.Decompress(bytBuffer);
                string strRecordData  = ASCIIEncoding.ASCII.GetString(bytRecordBytes);

                objRecords = CompressedFileRecordManager.FromString(strRecordData);
            }

            CompressedFileRecordManager = new CompressedFileRecordManager(intFileDataStartingIndex, objRecords, CompressedFileRecordState.Updated);
        }
Exemple #5
0
        private void ExtractFile(CompressedFileRecord objRecord, CompressedFileRecordState enuRecordState, Stream objTargetStream)
        {
            if (enuRecordState == CompressedFileRecordState.Inserted)
            {
                using (FileStream objDiskFileStream = new FileStream(objRecord.OriginalFilePath, FileMode.Open, FileAccess.Read))
                {
                    CompressionManager.CopyToStream(objDiskFileStream, objTargetStream);
                }
            }
            else
            {
                int intFileStartIndex = CompressedFileRecordManager.FindFileStartIndex(objRecord);
                if (intFileStartIndex == -1)
                {
                    throw new FileNotFoundException("Unable to locate '" + objRecord.RelativeFilePath + "' within package.");
                }

                CompressedStream.Position = intFileStartIndex;
                CompressionManager.Decompress(CompressedStream, objTargetStream, objRecord.Size);
            }
        }
Exemple #6
0
        public FileInfo ExtractFile(CompressedFileRecord objRecord, string strPath)
        {
            if (objRecord == null)
            {
                throw new ArgumentNullException("objRecord", "A valid non-null CompressedFileRecord is required.");
            }
            if (strPath == null)
            {
                throw new ArgumentNullException("strPath", "A valid non-null string is required.");
            }

            if (CompressedFileRecordManager.Exists(objRecord) == false)
            {
                throw new Exception("A record for '" + objRecord.RelativePath + "' could not be found.");
            }

            CompressedFileRecordState enuRecordState = CompressedFileRecordManager.GetRecordState(objRecord);

            if (enuRecordState == CompressedFileRecordState.Deleted)
            {
                throw new Exception("A record for '" + objRecord.RelativePath + "' could not be found.");
            }

            string strFilePath      = Path.Combine(strPath, objRecord.RelativeFilePath);
            string strDirectoryPath = Path.GetDirectoryName(strFilePath);

            if (Directory.Exists(strDirectoryPath) == false)
            {
                Directory.CreateDirectory(strDirectoryPath);
            }

            using (FileStream objFileStream = new FileStream(strFilePath, FileMode.Create, FileAccess.Write))
            {
                ExtractFile(objRecord, enuRecordState, objFileStream);
            }

            FileInfo objFileInfo = new FileInfo(strFilePath);

            return(objFileInfo);
        }
Exemple #7
0
        public CompressedFileRecord AddFile(string strFilePath, string strRelativePath)
        {
            if ((strFilePath == null) || (strFilePath.Length == 0))
            {
                throw new ArgumentOutOfRangeException("strFilePath", "A valid non-null, non-empty string is required.");
            }
            if (File.Exists(strFilePath) == false)
            {
                throw new FileNotFoundException("The source file could not be found.", strFilePath);
            }

            string strFormattedRelativePath = strRelativePath;

            if (Path.GetFileName(strFilePath).ToLower() == strRelativePath.ToLower())
            {
                strFormattedRelativePath = Path.GetDirectoryName(strRelativePath);
            }

            CompressedFileRecord objRecord = new CompressedFileRecord(strFilePath, 0, strFormattedRelativePath);

            CompressedFileRecordManager.Insert(objRecord);

            return(objRecord);
        }
Exemple #8
0
 public void Clear()
 {
     CompressedFileRecordManager.Clear();
 }
Exemple #9
0
 public CompressedFileRecord[] GetFilesByKey(string strKey)
 {
     return(CompressedFileRecordManager.GetRecordsByKey(strKey));
 }