public static void RestoreAs7ZipArchiveHelper(ImageReader openedReader, MetaRecord record, string targetFilename)
 {
     try
     {
         openedReader.Extract(record, targetFilename);
     }
     catch (Exception exc)
     {
         MessageBox.Show(exc.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
     }
 }
Exemple #2
0
        /// <summary>
        /// Searches for the anteposition of file
        /// </summary>
        /// <param name="record">record to search</param>
        /// <returns>anteposition</returns>
        /// <exception cref="InvalidOperationException">record not found</exception>
        public Int64 GetDisplacementOfPackedFile(MetaRecord record)
        {
            Int64 displacement = 0;

            foreach (MetaRecord recordIn in _records)
            {
                if (recordIn == record)
                {
                    return(displacement);
                }

                displacement += recordIn.SizeOfData;
            }
            throw new InvalidOperationException("Not found");
        }
        public void Extract(MetaRecord record, string destinationFileName)
        {
            if (File.Exists(destinationFileName))
            {
                File.Delete(destinationFileName);
            }

            Int64 displacement = _dataStartPos + _metadata.GetDisplacementOfPackedFile(record);

            using (FileStream fs = File.OpenRead(FileName))
            {
                fs.Seek(displacement, SeekOrigin.Begin);

                using (BinaryReader reader = new BinaryReader(fs))
                {
                    using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(destinationFileName)))
                    {
                        byte[] buffer = new byte[1];

                        Int64 sizeToRead = record.SizeOfData;

                        while (sizeToRead > 0)
                        {
                            if (buffer.Length == 0)
                            {
                                throw new InvalidDataException(_INVALIDIMAGE);
                            }

                            if (sizeToRead >= _BUFFERLENGTH)
                            {
                                buffer = reader.ReadBytes(_BUFFERLENGTH);
                            }
                            else
                            {
                                buffer = reader.ReadBytes((int)sizeToRead);
                            }

                            writer.Write(buffer);
                            sizeToRead -= buffer.Length;
                        }
                    }
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Loads metadata from an array
        /// </summary>
        /// <param name="bytes">Array of bytes</param>
        public void FromByteArray(byte[] array)
        {
            int recordsAmount  = BitConverter.ToInt32(array, 0);
            int LastByteIndex  = 4;
            int metarecordsize = -1;

            byte[] splitMe;

            for (int i = 0; i < recordsAmount; i++)
            {
                metarecordsize = BitConverter.ToInt32(array, LastByteIndex);
                LastByteIndex += 4;

                splitMe = new byte[metarecordsize];
                Array.Copy(array, LastByteIndex, splitMe, 0, metarecordsize);
                _records.Add(MetaRecord.FromByteArray(splitMe));

                LastByteIndex += metarecordsize;
            }
        }
        /// <summary>
        /// Creates an instance of MetaRecord
        /// </summary>
        /// <param name="ByteArray">metadata in an array</param>
        public static MetaRecord FromByteArray(byte[] array)
        {
            MetaRecord rm = new MetaRecord();

            int length = array.Length - 9;

            rm.IsFolder = Convert.ToBoolean(array[length]);

            char[] strarray = new char[1024];
            int    charUsed, bytesUsed;
            bool   isCompleted;

            Encoding.Default.GetDecoder().Convert(array, 0, length, strarray,
                                                  0, strarray.Length, true, out bytesUsed, out charUsed, out isCompleted);

            rm.InitialTarget = new string(strarray, 0, charUsed);

            rm._sizeOfData = BitConverter.ToInt64(array, length + 1);

            return(rm);
        }
        public static void RestoreToPointedFolderHelper(ImageReader openedReader, MetaRecord record, string pointedFolder, bool imageDataIsPasswordProtected, string password)
        {
            try
            {
                string tempFile = Path.GetTempFileName();

                UnpackParameters unpackParameters;
                if (imageDataIsPasswordProtected)
                {
                    unpackParameters = new UnpackParameters(password, tempFile, pointedFolder);
                }
                else
                {
                    unpackParameters = new UnpackParameters(tempFile, pointedFolder);
                }

                try
                {
                    openedReader.Extract(record, tempFile);

                    UnpackArchive aup = new UnpackArchive(unpackParameters);
                    aup.StartJob();
                }
                finally
                {
                    if (imageDataIsPasswordProtected)
                    {
                        BackupProcess.OverwriteFileWithZerosWithoutLogging(tempFile);
                    }

                    File.Delete(tempFile);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, string.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0);
            }
        }
        public static void RestoreToOriginalLocation(ImageReader openedReader, MetaRecord record, bool imageDataIsPasswordProtected, string password)
        {
            string folderThatHasThisFile = record.InitialTarget.Substring(0, record.InitialTarget.LastIndexOf("\\", StringComparison.InvariantCulture));

            RestoreToPointedFolderHelper(openedReader, record, folderThatHasThisFile, imageDataIsPasswordProtected, password);
        }