Example #1
0
        private bool Check(ArchiveInformation info, FileStream fs)
        {
            _fileInfo.Refresh();

            BinaryReader br = fs.GetBinaryReader();

            fs.Seek(-24, SeekOrigin.End);
            int oldSize = br.ReadInt32();

            if (oldSize != _fileInfo.Length)
            {
                return(false);
            }

            if (_fileInfo.LastWriteTimeUtc != info.FileTime)
            {
                return(false);
            }
            if (String.Compare(Path.GetFullPath(_fileInfo.FullName), Path.GetFullPath(info.ContentFilePath), StringComparison.InvariantCultureIgnoreCase) != 0)
            {
                return(false);
            }

            return(true);
        }
Example #2
0
        public void Write(ArchiveInformation info)
        {
            _fileInfo.Refresh();
            info.FileTime = _fileInfo.LastWriteTimeUtc;

            long position = FindPosition();

            using (FileStream fs = new FileStream(_fileInfo.FullName, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    if (position < 0)
                    {
                        position = _fileInfo.Length;
                    }

                    fs.Seek(position, SeekOrigin.Begin);

                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, info);

                    bw.Write((int)(fs.Position + 24));
                    bw.Write((int)(fs.Position - position));
                    bw.Write(MagicNumber);

                    fs.SetLength(fs.Position);
                }

            info.FileSize = _fileInfo.Length;
            _fileInfo.LastWriteTimeUtc = info.FileTime;
        }
Example #3
0
        public ArchiveInformation Read()
        {
            long position = FindPosition();

            if (position < 0)
            {
                return(null);
            }

            using (FileStream fs = new FileStream(_fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                fs.Seek(position, SeekOrigin.Begin);

                BinaryFormatter    bf   = new BinaryFormatter();
                ArchiveInformation info = (ArchiveInformation)bf.Deserialize(fs);
                info.FileSize = _fileInfo.Length;

                if (!Check(info, fs))
                {
                    return(null);
                }

                return(info);
            }
        }
Example #4
0
        public ArchiveUncompressor(ArchiveInformation info, Stream input, Stream output)
        {
            Exceptions.CheckArgumentNull(info, "info");
            Exceptions.CheckArgumentNull(input, "input");
            Exceptions.CheckArgumentNull(output, "output");

            _info   = info;
            _input  = input;
            _output = output;
        }
Example #5
0
        public void MoveFiles(ArchiveInformation target)
        {
            File.Delete(target.ContentFilePath);
            File.Delete(target.MetricFilePath);

            File.Move(ContentFilePath, target.ContentFilePath);
            File.Move(MetricFilePath, target.MetricFilePath);

            FilePath         = target.FilePath;
            RootArchive.Name = target.RootArchive.Name;
        }
Example #6
0
        private ArchiveInformation CreateTargetArchiveInformation()
        {
            ArchiveInformation result = _info.GetTempArchiveInformation();

            foreach (ArchiveFileEntry oldChild in _info.RootArchive.Childs.OfType <ArchiveFileEntry>())
            {
                MoveFile(oldChild, result.RootArchive, result.RootDirectory);
            }

            MoveArchive(_info.RootArchive, result.RootArchive, result.RootDirectory);
            return(result);
        }
Example #7
0
 private void WriteMetrics(ArchiveInformation targetInfo)
 {
     using (Stream output = new FileStream(targetInfo.MetricFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
         using (BinaryWriter bw = new BinaryWriter(output))
         {
             foreach (ArchiveFileEntry file in targetInfo.RootArchive.GetOrderedFileEntries().Values)
             {
                 bw.Write(file.UncompressedContentSize);
                 bw.Write(file.ContentOffset);
                 bw.Write((int)Compression.None);
             }
         }
 }
Example #8
0
        private void WriteContent(MemoryMappedFile memoryFile, Stream outputStream, ArchiveInformation inputInfo, ArchiveInformation outputInfo)
        {
            List <ArchiveFileEntry> inputFiles  = inputInfo.RootArchive.Expand();
            List <ArchiveFileEntry> outputFiles = outputInfo.RootArchive.Expand();

            for (int i = 0; i < inputFiles.Count; i++)
            {
                ArchiveFileEntry inputEntry  = inputFiles[i];
                ArchiveFileEntry outputEntry = outputFiles[i];

                if (inputEntry.Name != outputEntry.Name)
                {
                    throw new Exception("Порядок элементов в иерархиях изменился.");
                }

                using (Stream inputStream = memoryFile.CreateViewStream(inputEntry.GetAbsoluteOffset(), inputEntry.UncompressedContentSize))
                {
                    outputStream.Seek(outputEntry.GetAbsoluteOffset(), SeekOrigin.Begin);
                    inputStream.CopyTo(outputStream);
                }
                Progress.NullSafeInvoke(this, new ProgressArgs(_info.ContentFilePath, Interlocked.Add(ref _processedSize, inputEntry.UncompressedContentSize), _totalProgress));
            }
        }
Example #9
0
        public static int CalcUncompressedSize(ArchiveInformation info)
        {
            int result = 0;

            foreach (ArchiveArchiveEntry archive in info.RootArchive.Childs.OfType <ArchiveArchiveEntry>())
            {
                result += archive.MetricsEntry.UncompressedContentSize;
            }
            foreach (ArchiveArchiveEntry archive in info.RootArchive.Childs.OfType <ArchiveArchiveEntry>())
            {
                result += archive.ListingEntry.UncompressedContentSize;
            }
            foreach (ArchiveArchiveEntry archive in info.RootArchive.Childs.OfType <ArchiveArchiveEntry>())
            {
                result += archive.ContentEntry.UncompressedContentSize;
            }
            foreach (ArchiveFileEntry file in info.RootArchive.Childs.OfType <ArchiveFileEntry>())
            {
                result += file.UncompressedContentSize;
            }

            return(result);
        }
Example #10
0
        public ArchiveAnalizer(ArchiveInformation info)
        {
            Exceptions.CheckArgumentNull(info, "info");

            _info = info;
        }