Beispiel #1
0
        //Створення папки вручну
        protected DirElement(DirElement parent, SafeStreamAccess dataFileStream, string Name, Object addElementLocker, Object changeElementsLocker, Bitmap Icon = null,
                             SafeStreamAccess.ProgressCallback Progress = null) : this(addElementLocker, changeElementsLocker)
        {
            lock (_addElementLocker)
            {
                lock (dataFileStream.writeLock)
                {
                    header = new Header(parent.header.headersFileStream, parent.header.AES, ElementType.Dir);
                    this.dataFileStream = dataFileStream;

                    byte[] icon         = GetIconBytes(Icon);
                    UInt32 iconSize     = icon == null ? 0 : (UInt32)icon.Length;
                    UInt64 iconStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(iconSize));                     //Вибираємо місце куди писати іконку

                    AesCryptoServiceProvider AES = GetFileAES(_IconIV);

                    if ((icon != null) && (iconSize > 0))
                    {
                        dataFileStream.WriteEncrypt((long)iconStartPos, icon, AES);
                    }

                    _Name         = Name;
                    _ID           = GenID();
                    _ParentID     = parent.ID;
                    _IconStartPos = iconStartPos;
                    _IconSize     = iconSize;
                    _PHash        = GetPHash(Icon);
                    Parent        = parent;

                    SaveInf();
                }
            }
        }
Beispiel #2
0
 public override void SaveAs(string FullName, SafeStreamAccess.ProgressCallback Progress = null, Func <string, string> GetFileName = null)
 {
     Directory.CreateDirectory(Path.GetDirectoryName(FullName));
     using (FileStream stream = new FileStream(FullName, FileMode.Create, FileAccess.Write, FileShare.Read))
     {
         SaveTo(stream, Progress);
     }
 }
Beispiel #3
0
 public override void SaveTo(string PathToSave, SafeStreamAccess.ProgressCallback Progress = null)
 {
     Directory.CreateDirectory(PathToSave);
     using (FileStream stream = new FileStream(PathToSave + '\\' + _Name, FileMode.Create, FileAccess.Write, FileShare.Read))
     {
         SaveTo(stream, Progress);
     }
 }
Beispiel #4
0
        public void SaveTo(Stream stream, SafeStreamAccess.ProgressCallback Progress = null)
        {
            if (_FileSize == 0)
            {
                return;
            }

            AesCryptoServiceProvider AES = GetFileAES(_FileIV);

            //dataFileStream.ReadDecrypt((long)_FileStartPos, stream, (long)_FileSize, AES, Progress);
            dataFileStream.MultithreadDecrypt((long)_FileStartPos, stream, (long)_FileSize, AES, Progress);
            AES.Dispose();
        }
Beispiel #5
0
        public void ChangeContent(Stream NewData, SafeStreamAccess.ProgressCallback Progress = null)
        {
            lock (_addElementLocker)
            {
                UInt64 fileSize     = (UInt64)NewData.Length;
                UInt64 fileStartPos = _FileStartPos;
                if (fileSize >= Crypto.GetMod16(_FileSize))
                {
                    fileStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(fileSize));                     //Вибираємо місце куди писати файл
                }

                byte[] tempHash;

                try
                {
                    if (fileSize > 0)
                    {
                        AesCryptoServiceProvider AES = GetFileAES(_FileIV);
                        dataFileStream.WriteEncrypt((long)fileStartPos, NewData, AES, out tempHash, Progress);
                    }
                    else
                    {
                        tempHash = new byte[16];
                        CryptoRandom.GetBytes(tempHash);
                    }
                }
                catch
                {
                    throw new DataWasNotWrittenException("Файл не записався.");
                }

                try
                {
                    _FileStartPos = fileStartPos;
                    _FileSize     = fileSize;
                    _Hash         = tempHash;
                    SaveInf();
                }
                catch
                {
                    throw new HeaderWasNotWrittenException("Інформація про зміни не записалась!");
                }
            }
        }
Beispiel #6
0
        private void ExportDir(string destPath, string Name = "", SafeStreamAccess.ProgressCallback Progress = null, Func <string, string> GetFileName = null)
        {
            bool randomNames = (GetFileName != null);

            string tempName = Name == "" ? _Name : Name;

            Directory.CreateDirectory(destPath + '\\' + tempName);
            Element[] elementList;

            lock (_changeElementsLocker)
            {
                elementList = new Element[_Elements.Count];
                _Elements.CopyTo(elementList);
            }

            foreach (var element in elementList)
            {
                if (element is DirElement)
                {
                    if (randomNames)
                    {
                        (element as DirElement).ExportDir(destPath + '\\' + tempName, GetFileName(Path.GetExtension(element.Name)), Progress: Progress, GetFileName: GetFileName);
                    }
                    else
                    {
                        (element as DirElement).ExportDir(destPath + '\\' + tempName, Progress: Progress);
                    }
                }
                else
                {
                    if (randomNames)
                    {
                        element.SaveAs(destPath + '\\' + tempName + "\\" + GetFileName(Path.GetExtension(element.Name)), Progress);
                    }
                    else
                    {
                        element.SaveTo(destPath + '\\' + tempName, Progress);
                    }
                }
            }

            elementList = null;
        }
Beispiel #7
0
 public Element AddFile(string sourceFileName, string destFileName, bool compressFile = false, Bitmap Icon = null, SafeStreamAccess.ProgressCallback Progress = null)
 {
     try
     {
         using (FileStream f = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
         {
             return(AddFile(f, destFileName, compressFile, Icon, Progress, true));
         }
     }
     catch
     {
         return(null);
     }
 }
Beispiel #8
0
 public FileElement AddFile(Stream stream, string destFileName, bool compressFile = false, Bitmap Icon = null, SafeStreamAccess.ProgressCallback Progress = null)
 {
     return(AddFile(stream, destFileName, compressFile, Icon, Progress, true));
 }
Beispiel #9
0
        private FileElement AddFile(Stream stream, string destFileName, bool compressFile = false, Bitmap Icon = null, SafeStreamAccess.ProgressCallback Progress = null, bool isPrivate = true)
        {
            lock (_changeElementsLocker)
            {
                if (FindByName(_Elements, destFileName) != null)
                {
                    throw new DuplicatesFileNameException("Файл або папка з таким ім’ям вже є.");
                }
            }

            FileElement file;

            try
            {
                file = new FileElement(this, header, dataFileStream, destFileName, stream, compressFile, _addElementLocker, _changeElementsLocker, Icon, Progress);
            }
            catch
            {
                throw new DataWasNotWrittenException("Файл або іконка не записались.");
            }

            return(file);
        }
Beispiel #10
0
 public override void SaveAs(string FullName, SafeStreamAccess.ProgressCallback Progress = null, Func <string, string> GetFileName = null)
 {
     ExportDir(Path.GetDirectoryName(FullName), Path.GetFileName(FullName), Progress: Progress, GetFileName: GetFileName);
 }
Beispiel #11
0
 public override void SaveTo(string PathToSave, SafeStreamAccess.ProgressCallback Progress = null)
 {
     ExportDir(PathToSave, Progress: Progress);
 }
Beispiel #12
0
 public abstract void SaveAs(string FullName, SafeStreamAccess.ProgressCallback Progress = null, Func <string, string> GetFileName = null);
Beispiel #13
0
        //protected abstract void ChangeParent(DirElement NewParent);

        public abstract void SaveTo(string PathToSave, SafeStreamAccess.ProgressCallback Progress = null);
Beispiel #14
0
        //Створення файлу вручну
        public FileElement(DirElement parent, Header parentHeader, SafeStreamAccess dataFileStream, string Name, Stream fileStream, bool isCompressed,
                           Object addElementLocker, Object changeElementsLocker, Bitmap Icon = null, SafeStreamAccess.ProgressCallback Progress = null) : base(addElementLocker, changeElementsLocker)
        {
            lock (_addElementLocker)
            {
                UInt64 fileSize = (UInt64)fileStream.Length;

                byte[] icon     = GetIconBytes(Icon);
                UInt32 iconSize = icon == null ? 0 : (UInt32)icon.Length;

                UInt64 fileStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(fileSize));                 //Вибираємо місце куди писати файл
                UInt64 iconStartPos = dataFileStream.GetFreeSpaceStartPos(Crypto.GetMod16(iconSize));                 //Вибираємо місце куди писати іконку
                iconStartPos = (iconStartPos == fileStartPos) ? iconStartPos += Crypto.GetMod16(fileSize) : iconStartPos;

                lock (dataFileStream.writeLock)
                {
                    header = new Header(parentHeader.headersFileStream, parentHeader.AES, ElementType.File);
                    this.dataFileStream = dataFileStream;

                    _Name         = Name;
                    _ParentID     = parent.ID;
                    _FileStartPos = fileStartPos;
                    _FileSize     = fileSize;
                    _IconStartPos = iconStartPos;
                    _IconSize     = iconSize;
                    _IsCompressed = isCompressed;
                    _Hash         = new byte[16];
                    CryptoRandom.GetBytes(_Hash);
                    _PHash = GetPHash(Icon);

                    SaveInf();
                    _Exists = false;
                }

                AesCryptoServiceProvider AES = GetFileAES(_FileIV);

                if (fileSize > 0)
                {
                    dataFileStream.WriteEncrypt((long)fileStartPos, fileStream, AES, out _Hash, Progress);
                }

                if ((icon != null) && (iconSize > 0))
                {
                    AES.IV = _IconIV;
                    dataFileStream.WriteEncrypt((long)iconStartPos, icon, AES);
                }

                //Закидаємо файл в потрібну папку і записуємо зміни
                _Exists = true;
                ChangeParent(parent, true);
            }
        }