Пример #1
0
 public ZipContainer(SevenZipExtractor zip, ArchiveFileInfo entry, ZipContainer parent)
 {
     _name = entry.FileName;
     _entry = entry;
     Zip = zip;
     _parent = parent;
 }
Пример #2
0
 internal ArchiveEntry(ArchiveFileInfo archiveEntry, string archivePath, InArchiveFormat archiveFormat)
 {
     Index = (uint) archiveEntry.Index;
     Path = archiveEntry.FileName;
     Size = archiveEntry.Size;
     Name = System.IO.Path.GetFileName(archiveEntry.FileName);
     CompressedSize = 0; // Not supported in SevenZipSharp (yet)
     ModifiedDate = archiveEntry.LastWriteTime;
     IsEncrypted = archiveEntry.Encrypted;
     IsFolder = archiveEntry.IsDirectory;
     ArchivePath = archivePath;
     Format = archiveFormat;
     CRC = archiveEntry.Crc;
 }
 private IEnumerable<ArchiveFileInfo> GetArchiveFileInfos()
 {
     var archives = new ArchiveFileInfo[(AdditionalArgs.Count + 1) / 3];
     for (int i = 0; i < archives.Length; i++)
     {
         archives[i] = new ArchiveFileInfo
         {
             Path = Path.GetFullPath(AdditionalArgs[i * 3 + 1]),
             SubDir = (AdditionalArgs.Count > i * 3 + 2) ? AdditionalArgs[i * 3 + 2] : null,
             MimeType = (AdditionalArgs.Count > i * 3 + 3) ? AdditionalArgs[i * 3 + 3] : Archive.GuessMimeType(AdditionalArgs[i * 3 + 1])
         };
     }
     return archives;
 }
Пример #4
0
 public void ReplaceFile(ArchiveFileInfo afi, Stream fileData)
 {
     afi.SetFileData(fileData);
 }
Пример #5
0
 public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
 {
     files.Remove((APAKFileInfo)archiveFileInfo);
     return(true);
 }
Пример #6
0
 /// <inheritdoc />
 public Task <LoadResult> LoadFile(IStateInfo stateInfo, ArchiveFileInfo afi, Guid pluginId)
 {
     return(LoadFile(stateInfo, afi, pluginId, new LoadFileContext()));
 }
Пример #7
0
 /// <inheritdoc />
 public Task <LoadResult> LoadFile(IStateInfo stateInfo, ArchiveFileInfo afi, Guid pluginId, LoadFileContext loadFileContext)
 {
     return(_parentPluginManager.LoadFile(stateInfo, afi, pluginId, loadFileContext));
 }
Пример #8
0
        private FileSystemNode GetContent(ArchiveFileInfo entry)
        {
            if(Path.GetExtension(entry.FileName) == ".zip" || Path.GetExtension(entry.FileName) == ".7z")
            {
                // extract the embedded zip and create a new zip container for it with a reference to the parent.
                // if we need to we'll leave the contents un-extracted so we can extract that at will later
                return new ZipContainer(new SevenZipExtractor(ExtractEntry(Zip, entry)), entry, this);
            }

            return new ZipContent(Zip, entry);
        }
        /// <summary>
        /// Retrieves all information about the archive.
        /// </summary>
        /// <exception cref="SevenZip.SevenZipArchiveException"/>
        private void GetArchiveInfo(bool disposeStream)
        {
            if (_archive == null)
            {
                if (!ThrowException(null, new SevenZipArchiveException()))
                {
                    return;
                }
            }
            else
            {
                IInStream archiveStream;
                using ((archiveStream = GetArchiveStream(disposeStream)) as IDisposable)
                {
                    var openCallback = GetArchiveOpenCallback();
                    if (!_opened)
                    {
                        if (!OpenArchive(archiveStream, openCallback))
                        {
                            return;
                        }
                        _opened = !disposeStream;
                    }
                    _filesCount = _archive.GetNumberOfItems();
                    _archiveFileData = new List<ArchiveFileInfo>((int)_filesCount);
                    if (_filesCount != 0)
                    {
                        var data = new PropVariant();
                        try
                        {
                            #region Getting archive items data

                            for (uint i = 0; i < _filesCount; i++)
                            {
                                try
                                {
                                    var fileInfo = new ArchiveFileInfo { Index = (int)i };
                                    _archive.GetProperty(i, ItemPropId.Path, ref data);
                                    fileInfo.FileName = NativeMethods.SafeCast(data, "[no name]");
                                    _archive.GetProperty(i, ItemPropId.LastWriteTime, ref data);
                                    fileInfo.LastWriteTime = NativeMethods.SafeCast(data, DateTime.Now);
                                    _archive.GetProperty(i, ItemPropId.CreationTime, ref data);
                                    fileInfo.CreationTime = NativeMethods.SafeCast(data, DateTime.Now);
                                    _archive.GetProperty(i, ItemPropId.LastAccessTime, ref data);
                                    fileInfo.LastAccessTime = NativeMethods.SafeCast(data, DateTime.Now);
                                    _archive.GetProperty(i, ItemPropId.Size, ref data);
                                    fileInfo.Size = NativeMethods.SafeCast<ulong>(data, 0);
                                    if (fileInfo.Size == 0)
                                    {
                                        fileInfo.Size = NativeMethods.SafeCast<uint>(data, 0);
                                    }
                                    _archive.GetProperty(i, ItemPropId.Attributes, ref data);
                                    fileInfo.Attributes = NativeMethods.SafeCast<uint>(data, 0);
                                    _archive.GetProperty(i, ItemPropId.IsDirectory, ref data);
                                    fileInfo.IsDirectory = NativeMethods.SafeCast(data, false);
                                    _archive.GetProperty(i, ItemPropId.Encrypted, ref data);
                                    fileInfo.Encrypted = NativeMethods.SafeCast(data, false);
                                    _archive.GetProperty(i, ItemPropId.Crc, ref data);
                                    fileInfo.Crc = NativeMethods.SafeCast<uint>(data, 0);
                                    _archive.GetProperty(i, ItemPropId.Comment, ref data);
                                    fileInfo.Comment = NativeMethods.SafeCast(data, "");
                                    _archiveFileData.Add(fileInfo);
                                }
                                catch (InvalidCastException)
                                {
                                    ThrowException(null, new SevenZipArchiveException("probably archive is corrupted."));
                                }
                            }

                            #endregion

                            #region Getting archive properties

                            uint numProps = _archive.GetNumberOfArchiveProperties();
                            var archProps = new List<ArchiveProperty>((int)numProps);
                            for (uint i = 0; i < numProps; i++)
                            {
                                string propName;
                                ItemPropId propId;
                                ushort varType;
                                _archive.GetArchivePropertyInfo(i, out propName, out propId, out varType);
                                _archive.GetArchiveProperty(propId, ref data);
                                if (propId == ItemPropId.Solid)
                                {
                                    _isSolid = NativeMethods.SafeCast(data, true);
                                }
                                // TODO Add more archive properties
                                if (PropIdToName.PropIdNames.ContainsKey(propId))
                                {
                                    archProps.Add(new ArchiveProperty
                                    {
                                        Name = PropIdToName.PropIdNames[propId],
                                        Value = data.Object
                                    });
                                }
                                else
                                {
                                    Debug.WriteLine(
                                        "An unknown archive property encountered (code " +
                                        ((int)propId).ToString(CultureInfo.InvariantCulture) + ')');
                                }
                            }
                            _archiveProperties = new ReadOnlyCollection<ArchiveProperty>(archProps);
                            if (!_isSolid.HasValue && _format == InArchiveFormat.Zip)
                            {
                                _isSolid = false;
                            }
                            if (!_isSolid.HasValue)
                            {
                                _isSolid = true;
                            }

                            #endregion
                        }
                        catch (Exception)
                        {
                            if (openCallback.ThrowException())
                            {
                                throw;
                            }
                        }
                    }
                }
                if (disposeStream)
                {
                    _archive.Close();
                    _archiveStream = null;
                }
                _archiveFileInfoCollection = new ReadOnlyCollection<ArchiveFileInfo>(_archiveFileData);
            }
        }
Пример #10
0
 public bool AddFile(ArchiveFileInfo afi)
 {
     _sarc.Files.Add(afi);
     return(true);
 }
Пример #11
0
        public void WriteFile(string filepath, bool compressNOTWORKING, ArchiveFile file, bool surpressErrors)
        {
            if (!_writing)
            {
                throw new Exception("Must call BeginWriting() before calling WriteFile()");
            }

            if (!_indexTable.ContainsKey(filepath))
            {
                if (!surpressErrors)
                {
                    throw new FileNotFoundException("File with given path was not found in the releasemanifest");
                }

                Console.WriteLine("Following file was not found in the releasemanifest: {0}", filepath);
                return;
            }

            if (!_fileTable.ContainsKey(filepath))
            {
                if (!surpressErrors)
                {
                    throw new FileNotFoundException("File with given path was not found in the file table");
                }

                Console.WriteLine("Following file was not found: {0}", filepath);
                return;
            }

            var archive = _fileTable[filepath];

            for (int i = 0; i < archive.Count; i++)
            {
                // Handle archive state creation
                if (!_archiveStates.ContainsKey(archive[i].FilePath))
                {
                    var state = new ArchiveState();
                    state.ArchivePath    = archive[i].FilePath;
                    state.OriginalLength = archive[i].DataLength;
                    state.OriginalValues = new Dictionary <string, ArchiveFileInfo>();
                    _archiveStates[archive[i].FilePath] = state;
                }

                if (!_bufferTable.ContainsKey(archive[i].FilePath))
                {
                    _bufferTable[archive[i].FilePath] = new ArchiveWriteBuffer();
                }

                _bufferTable[archive[i].FilePath].WriteData(filepath, file.Data);

                // Copy file info to the list of originals and then modify it
                if (!_archiveStates[archive[i].FilePath].OriginalValues.ContainsKey(filepath))
                {
                    _archiveStates[archive[i].FilePath].OriginalValues[filepath] = ArchiveFileInfo.Copy(archive[i].Files[filepath]);
                }
            }

            // Handle manifest changes
            _indexTable[filepath].Descriptor.CompressedSize   = file.CompressedSize;
            _indexTable[filepath].Descriptor.DecompressedSize = file.UncompressedSize;
        }
Пример #12
0
 public void LoadFile(ArchiveFileInfo archiveFileInfo)
 {
     ArchiveFileInfo = archiveFileInfo;
 }
Пример #13
0
 public FileSystemPath GetVirtualFilePath(ArchiveFileInfo archiveFile)
 {
     string path = FileSystemPath.DirectorySeparator + archiveFile.FileName.Replace(Path.DirectorySeparatorChar, FileSystemPath.DirectorySeparator);
     if (archiveFile.IsDirectory && path[path.Length - 1] != FileSystemPath.DirectorySeparator)
         path += FileSystemPath.DirectorySeparator;
     return FileSystemPath.Parse(path);
 }
Пример #14
0
 public void LoadFile(ArchiveFileInfo archiveFileInfo, IArchiveFile archiveFile)
 {
     ArchiveFileInfo = archiveFileInfo;
     ArchiveFile     = archiveFile;
 }
Пример #15
0
 public OpenTabEventArgs(ArchiveFileInfo afi, KoreFileInfo kfi, BaseReadOnlyDirectoryNode fs)
 {
     Afi        = afi;
     Kfi        = kfi;
     FileSystem = fs;
 }
Пример #16
0
        /// <summary>
        /// Extracts <see cref="Archive"/>s to the <see cref="_store"/>.
        /// </summary>
        /// <param name="archives">The archives to extract over each other in order.</param>
        /// <param name="files">The downloaded archive files, indexes matching those in <paramref name="archives"/>.</param>
        /// <param name="manifestDigest">The digest the extracted archives should produce.</param>
        /// <exception cref="OperationCanceledException">An IO task was canceled from another thread.</exception>
        /// <exception cref="NotSupportedException">A file format, protocal, etc. is unknown or not supported.</exception>
        /// <exception cref="IOException">A downloaded file could not be written to the disk or extracted.</exception>
        /// <exception cref="ImplementationAlreadyInStoreException">There is already an <see cref="Store.Model.Implementation"/> with the specified <paramref name="manifestDigest"/> in the store.</exception>
        /// <exception cref="UnauthorizedAccessException">Write access to <see cref="IStore"/> is not permitted.</exception>
        /// <exception cref="DigestMismatchException">An <see cref="Store.Model.Implementation"/>'s <see cref="Archive"/>s don't match the associated <see cref="ManifestDigest"/>.</exception>
        private void ApplyArchives([NotNull, ItemNotNull] IList<Archive> archives, [NotNull, ItemNotNull] IList<TemporaryFile> files, ManifestDigest manifestDigest)
        {
            var archiveFileInfos = new ArchiveFileInfo[archives.Count];
            for (int i = 0; i < archiveFileInfos.Length; i++)
            {
                archiveFileInfos[i] = new ArchiveFileInfo
                {
                    Path = files[i].Path,
                    SubDir = archives[i].Extract,
                    Destination = archives[i].Destination,
                    MimeType = archives[i].MimeType,
                    StartOffset = archives[i].StartOffset,
                    OriginalSource = archives[i].Href
                };
            }

            _store.AddArchives(archiveFileInfos, manifestDigest, Handler);
        }
Пример #17
0
 public bool AddFile(ArchiveFileInfo afi) => false;
Пример #18
0
 public FileData(string srcFileName, ArchiveFileInfo fileInfo)
 {
     archiveFileName  = srcFileName;
     sevenZipFileInfo = fileInfo;
 }
        /// <summary>
        /// Modifies the existing archive (renames files or deletes them).
        /// </summary>
        /// <param name="archiveName">The archive file name.</param>
        /// <param name="newFileNames">New file names. Null value to delete the corresponding index.</param>
        /// <param name="password">The archive password.</param>
        public void ModifyArchive(string archiveName, Dictionary<int, string> newFileNames, string password 
#if CS4 
            = ""
#endif
        )
        {
            ClearExceptions();
            if (!SevenZipLibraryManager.ModifyCapable)
            {
                throw new SevenZipLibraryException("The specified 7zip native library does not support this method.");
            }
            if (!File.Exists(archiveName))
            {
                if (!ThrowException(null, new ArgumentException("The specified archive does not exist.", "archiveName")))
                {
                    return;
                }
            }
            if (newFileNames == null || newFileNames.Count == 0)
            {
                if (!ThrowException(null, new ArgumentException("Invalid new file names.", "newFileNames")))
                {
                    return;
                }
            }
            try
            {
                using (var extr = new SevenZipExtractor(archiveName))
                {
                    _updateData = new UpdateData();
                    var archiveData = new ArchiveFileInfo[extr.ArchiveFileData.Count];
                    extr.ArchiveFileData.CopyTo(archiveData, 0);
                    _updateData.ArchiveFileData = new List<ArchiveFileInfo>(archiveData);
                }
                _updateData.FileNamesToModify = newFileNames;
                _updateData.Mode = InternalCompressionMode.Modify;
            }
            catch (SevenZipException e)
            {
                if (!ThrowException(null, e))
                {
                    return;
                }
            }
            try
            {
                ISequentialOutStream sequentialArchiveStream;
                _compressingFilesOnDisk = true;
                using ((sequentialArchiveStream = GetOutStream(GetArchiveFileStream(archiveName))) as IDisposable)
                {
                    IInStream inArchiveStream;
                    _archiveName = archiveName;
                    using ((inArchiveStream = GetInStream()) as IDisposable)
                    {
                        IOutArchive outArchive;
                        // Create IInArchive, read it and convert to IOutArchive
                        SevenZipLibraryManager.LoadLibrary(
                            this, Formats.InForOutFormats[_archiveFormat]);
                        if ((outArchive = MakeOutArchive(inArchiveStream)) == null)
                        {
                            return;
                        }
                        using (ArchiveUpdateCallback auc = GetArchiveUpdateCallback(null, 0, password))
                        {
                            UInt32 deleteCount = 0;
                            if (_updateData.FileNamesToModify != null)
                            {
#if CS4 // System.Linq of C# 4 is great
                                deleteCount = (UInt32)_updateData.FileNamesToModify.Sum(
                                    pairDeleted => pairDeleted.Value == null ? 1 : 0);
#else
                                foreach(var pairDeleted in _updateData.FileNamesToModify)
                                {
                                    if (pairDeleted.Value == null)
                                    {
                                        deleteCount++;
                                    }
                                }
#endif
                            }
                            try
                            {
                                CheckedExecute(
                                    outArchive.UpdateItems(
                                        sequentialArchiveStream, _oldFilesCount - deleteCount, auc),
                                    SevenZipCompressionFailedException.DEFAULT_MESSAGE, auc);
                            }
                            finally
                            {
                                FreeCompressionCallback(auc);
                            }
                        }
                    }
                }
            }
            finally
            {
                SevenZipLibraryManager.FreeLibrary(this, Formats.InForOutFormats[_archiveFormat]);
                File.Delete(archiveName);
                FinalizeUpdate();
                _compressingFilesOnDisk = false;
                _updateData.FileNamesToModify = null;
                _updateData.ArchiveFileData = null;
                OnEvent(CompressionFinished, EventArgs.Empty, false);
            }
            ThrowUserException();
        }
Пример #20
0
 public void FileWriteAsync(ArchiveFileInfo fileInfo, string filePath)
 {
     fileInfo.DecompressData(fileInfo.FileData).SaveToFile(filePath);
 }
Пример #21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ExtractFileCallbackArgs"/> class.
 /// </summary>
 /// <param name="archiveFileInfo">The information about file in the archive.</param>
 public ExtractFileCallbackArgs(ArchiveFileInfo archiveFileInfo)
 {
     Reason = ExtractFileCallbackReason.Start;
     _archiveFileInfo = archiveFileInfo;
 }
Пример #22
0
        /// <summary>
        /// Begins the file extraction.
        /// </summary>
        /// <param name="file">The file within the archive.</param>
        private void ExtractFile(ArchiveFileInfo file)
        {
            _ext = Path.Combine(Path.GetDirectoryName(_file), Path.GetFileName(file.FileName));

            try
            {
                using (var fs = File.Create(_ext))
                {
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Warn("Failed to create new file to '" + _ext + "', retrying with temp path...", ex);

                _ext = Path.Combine(Path.GetTempPath(), Path.GetFileName(file.FileName));

                try
                {
                    using (var fs = File.Create(_ext))
                    {
                        fs.Close();
                    }
                }
                catch (Exception ex2)
                {
                    Log.Warn("Failed to create new temp file to '" + _ext + "', aborting extraction...", ex2);

                    TaskDialog.Show(new TaskDialogOptions
                    {
                        MainIcon                = VistaTaskDialogIcon.Error,
                        Title                   = "Extraction error",
                        MainInstruction         = "Extraction error",
                        Content                 = "Failed to create new file near archive or in the %TEMP% directory.",
                        AllowDialogCancellation = true,
                        CustomButtons           = new[] { "OK" }
                    });

                    return;
                }
            }

            _active = true;
            _tdstr  = "Extracting file...";
            var mthd = new Thread(() => TaskDialog.Show(new TaskDialogOptions
            {
                Title           = "Extracting...",
                MainInstruction = Path.GetFileName(file.FileName),
                Content         = _tdstr,
                CustomButtons   = new[]
                {
                    "&Run now",         // ID 500
                    "&Cancel"           // ID 501
                },
                ShowProgressBar         = true,
                EnableCallbackTimer     = true,
                AllowDialogCancellation = true,
                Callback = (dialog, args, data) =>
                {
                    dialog.SetProgressBarPosition(_tdpos);
                    dialog.SetContent(_tdstr);

                    if (args.ButtonId != 0)
                    {
                        if (args.ButtonId == 500)
                        {
                            if (File.Exists(_ext))
                            {
                                try { Utils.Run(_ext); } catch { }
                                _ran = true;
                            }
                        }
                        else if (args.ButtonId == 501)
                        {
                            if (_active)
                            {
                                _cancel = true;

                                if (!string.IsNullOrWhiteSpace(_ext) && File.Exists(_ext))
                                {
                                    new Thread(() =>
                                    {
                                        try { _thd.Abort(); _thd = null; } catch { }
                                        Thread.Sleep(100);
                                        try { _fs.Close(); _fs.Dispose(); } catch { }
                                        Thread.Sleep(100);
                                        try { File.Delete(_ext); } catch { }
                                    }).Start();
                                }
                            }

                            return(false);
                        }
                    }

                    if (!_active)
                    {
                        dialog.ClickButton(500);
                        return(false);
                    }

                    return(true);
                }
            }));

            mthd.SetApartmentState(ApartmentState.STA);
            mthd.Start();

            var total = file.Size;
            var last  = DateTime.MinValue;

            _thd = new Thread(() =>
            {
                using (var fs = _fs = File.Open(_ext, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                    using (var archive = new SevenZipExtractor(_file))
                    {
                        archive.Extracting += (sender, args) =>
                        {
                            if (_cancel)
                            {
                                args.Cancel = true;
                                return;
                            }

                            if (args.PercentDone == 100)
                            {
                                return;
                            }

                            if ((DateTime.Now - last).TotalMilliseconds < 150)
                            {
                                return;
                            }

                            last = DateTime.Now;

                            _tdpos = args.PercentDone;
                            _tdstr = "Extracting file: " + Utils.GetFileSize((long)Math.Round((args.PercentDone / 100d) * total)) + " / " + args.PercentDone + "% done...";
                        };

                        archive.ExtractionFinished += (sender, args) =>
                        {
                            _active = false;

                            if (!File.Exists(_ext))
                            {
                                return;
                            }

                            new Thread(() =>
                            {
                                if (!_ran || (_ran && !Utils.IsFileLocked(_ext)))
                                {
                                    Thread.Sleep(250);
                                    Utils.Run(_ext);
                                }

                                Thread.Sleep(10000);
                                AskAfterUse();
                            }).Start();
                        };

                        try
                        {
                            archive.ExtractFile(file.Index, fs);
                        }
                        catch (Exception ex)
                        {
                            Log.Warn("Error during file extraction.", ex);
                        }
                    }

                if (_cancel && File.Exists(_ext))
                {
                    try { File.Delete(_ext); } catch { }
                }
            });

            _thd.Start();
        }
Пример #23
0
 public ZipContent(SevenZipExtractor zip, ArchiveFileInfo entry)
 {
     _zip = zip;
     Entry = entry;
 }
Пример #24
0
 public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
 {
     files.Remove((SarcEntry)archiveFileInfo);
     return(true);
 }
Пример #25
0
 public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
 {
     Header.Files.Remove((TMPK_Parser.FileEntry)archiveFileInfo);
     return(true);
 }
Пример #26
0
 /// <summary>
 /// Refreshes the information in this object with new data retrieved
 /// from an archive.
 /// </summary>
 /// <param name="newFileInfo">Fresh instance for the same file just
 /// read from the archive.</param>
 /// <remarks>
 /// This implementation refreshes the <see cref="CabinetFolderNumber"/>.
 /// </remarks>
 protected override void Refresh(ArchiveFileInfo newFileInfo)
 {
     base.Refresh(newFileInfo);
     this.cabFolder = ((CabFileInfo)newFileInfo).cabFolder;
 }
Пример #27
0
 /// <inheritdoc />
 public Task <LoadResult> LoadFile(IStateInfo stateInfo, ArchiveFileInfo afi)
 {
     return(_parentPluginManager.LoadFile(stateInfo, afi));
 }
Пример #28
0
 public bool DeleteFile(ArchiveFileInfo archiveFileInfo)
 {
     return(false);
 }
 /// <summary>
 /// Initializes a new instance of the FileInfoEventArgs class.
 /// </summary>
 /// <param name="fileInfo">The current ArchiveFileInfo.</param>
 /// <param name="percentDone">The percent of finished work.</param>
 public FileInfoEventArgs(ArchiveFileInfo fileInfo, byte percentDone)
     : base(percentDone)
 {
     _fileInfo = fileInfo;
 }
            private string GetFileName(ArchiveFileInfo afi)
            {
                if ((!_Deploy) && (afi.IsDirectory))
                    return null;

                string path = afi.SafePath();

                string TRP = TargetRelativePath(path, _Deploy);

                if (TRP == null)
                    return null;

                if (afi.IsDirectory)
                {
                    if (_Deploy)
                        Directory.CreateDirectory(Path.Combine(_Root, TRP));

                    return null;
                }

                string resultDir = Path.Combine(_Root, Path.GetDirectoryName(TRP) ?? string.Empty );

                if (_Deploy)
                {
                    Directory.CreateDirectory(resultDir);
                }

                return FileInternalToolBox.CreateNewAvailableName(resultDir, Path.GetFileNameWithoutExtension(TRP), Path.GetExtension(TRP));
            }
Пример #31
0
 public bool AddFile(ArchiveFileInfo archiveFileInfo)
 {
     return(false);
 }
Пример #32
0
 public AfiFileNode(string name, ArchiveFileInfo afi) : base(name)
 {
     ArchiveFileInfo = afi ?? throw new ArgumentNullException(nameof(afi));
 }
Пример #33
0
 public bool DeleteFile(ArchiveFileInfo afi) => throw new NotSupportedException();
        /// <summary>
        /// Begins the file extraction.
        /// </summary>
        /// <param name="file">The file within the archive.</param>
        private void ExtractFile(ArchiveFileInfo file)
        {
            _ext = Path.Combine(Path.GetDirectoryName(_file), Path.GetFileName(file.FileName));

            try
            {
                using (var fs = File.Create(_ext))
                {
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                Log.Warn("Failed to create new file to '" + _ext + "', retrying with temp path...", ex);

                _ext = Path.Combine(Path.GetTempPath(), Path.GetFileName(file.FileName));

                try
                {
                    using (var fs = File.Create(_ext))
                    {
                        fs.Close();
                    }
                }
                catch (Exception ex2)
                {
                    Log.Warn("Failed to create new temp file to '" + _ext + "', aborting extraction...", ex2);

                    TaskDialog.Show(new TaskDialogOptions
                            {
                                MainIcon                = VistaTaskDialogIcon.Error,
                                Title                   = "Extraction error",
                                MainInstruction         = "Extraction error",
                                Content                 = "Failed to create new file near archive or in the %TEMP% directory.",
                                AllowDialogCancellation = true,
                                CustomButtons           = new[] { "OK" }
                            });

                    return;
                }
            }

            _active = true;
            _tdstr = "Extracting file...";
            var mthd = new Thread(() => TaskDialog.Show(new TaskDialogOptions
                {
                    Title                   = "Extracting...",
                    MainInstruction         = Path.GetFileName(file.FileName),
                    Content                 = _tdstr,
                    CustomButtons           = new[]
                        {
                            "&Run now", // ID 500
                            "&Cancel"   // ID 501
                        },
                    ShowProgressBar         = true,
                    EnableCallbackTimer     = true,
                    AllowDialogCancellation = true,
                    Callback                = (dialog, args, data) =>
                        {
                            dialog.SetProgressBarPosition(_tdpos);
                            dialog.SetContent(_tdstr);

                            if (args.ButtonId != 0)
                            {
                                if (args.ButtonId == 500)
                                {
                                    if (File.Exists(_ext))
                                    {
                                        try { Utils.Run(_ext); } catch { }
                                        _ran = true;
                                    }
                                }
                                else if (args.ButtonId == 501)
                                {
                                    if (_active)
                                    {
                                        _cancel = true;

                                        if (!string.IsNullOrWhiteSpace(_ext) && File.Exists(_ext))
                                        {
                                            new Thread(() =>
                                                {
                                                    try { _thd.Abort(); _thd = null; } catch { }
                                                    Thread.Sleep(100);
                                                    try { _fs.Close(); _fs.Dispose(); } catch { }
                                                    Thread.Sleep(100);
                                                    try { File.Delete(_ext); } catch { }
                                                }).Start();
                                        }
                                    }

                                    return false;
                                }
                            }

                            if (!_active)
                            {
                                dialog.ClickButton(500);
                                return false;
                            }

                            return true;
                        }
                }));
            mthd.SetApartmentState(ApartmentState.STA);
            mthd.Start();

            var total = file.Size;
            var last = DateTime.MinValue;

            _thd = new Thread(() =>
                {
                    using (var fs = _fs = File.Open(_ext, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                    using (var archive = new SevenZipExtractor(_file))
                    { 
                        archive.Extracting += (sender, args) =>
                            {
                                if (_cancel)
                                {
                                    args.Cancel = true;
                                    return;
                                }

                                if (args.PercentDone == 100)
                                {
                                    return;
                                }

                                if ((DateTime.Now - last).TotalMilliseconds < 150)
                                {
                                    return;
                                }

                                last = DateTime.Now;

                                _tdpos = args.PercentDone;
                                _tdstr = "Extracting file: " + Utils.GetFileSize((long)Math.Round((args.PercentDone / 100d) * total)) + " / " + args.PercentDone + "% done...";
                            };

                        archive.ExtractionFinished += (sender, args) =>
                            {
                                _active = false;

                                if (!File.Exists(_ext))
                                {
                                    return;
                                }

                                new Thread(() =>
                                    {
                                        if (!_ran || (_ran && !Utils.IsFileLocked(_ext)))
                                        {
                                            Thread.Sleep(250);
                                            Utils.Run(_ext);
                                        }
 
                                        Thread.Sleep(10000);
                                        AskAfterUse();
                                    }).Start();
                            };

                        try
                        {
                            archive.ExtractFile(file.Index, fs);
                        }
                        catch (Exception ex)
                        {
                            Log.Warn("Error during file extraction.", ex);
                        }
                    }

                    if (_cancel && File.Exists(_ext))
                    {
                        try { File.Delete(_ext); } catch { }
                    }
                });

            _thd.Start();
        }
        private static Mod AttemptLoadVirtualMod(ArchiveFileInfo sfarEntry, SevenZipExtractor archive, Mod.MEGame game, string md5)
        {
            var sfarPath   = sfarEntry.FileName;
            var cookedPath = FilesystemInterposer.DirectoryGetParent(sfarPath, true);

            //Todo: Check if value is CookedPC/CookedPCConsole as further validation
            if (!string.IsNullOrEmpty(FilesystemInterposer.DirectoryGetParent(cookedPath, true)))
            {
                var dlcDir        = FilesystemInterposer.DirectoryGetParent(cookedPath, true);
                var dlcFolderName = Path.GetFileName(dlcDir);
                if (!string.IsNullOrEmpty(dlcFolderName))
                {
                    var thirdPartyInfo = ThirdPartyServices.GetThirdPartyModInfo(dlcFolderName, game);
                    if (thirdPartyInfo != null)
                    {
                        if (thirdPartyInfo.PreventImport == false)
                        {
                            Log.Information($@"Third party mod found: {thirdPartyInfo.modname}, preparing virtual moddesc.ini");
                            //We will have to load a virtual moddesc. Since Mod constructor requires reading an ini, we will build and feed it a virtual one.
                            IniData virtualModDesc = new IniData();
                            virtualModDesc[@"ModManager"][@"cmmver"]     = App.HighestSupportedModDesc.ToString();
                            virtualModDesc[@"ModManager"][@"importedby"] = App.BuildNumber.ToString();
                            virtualModDesc[@"ModInfo"][@"game"]          = @"ME3";
                            virtualModDesc[@"ModInfo"][@"modname"]       = thirdPartyInfo.modname;
                            virtualModDesc[@"ModInfo"][@"moddev"]        = thirdPartyInfo.moddev;
                            virtualModDesc[@"ModInfo"][@"modsite"]       = thirdPartyInfo.modsite;
                            virtualModDesc[@"ModInfo"][@"moddesc"]       = thirdPartyInfo.moddesc;
                            virtualModDesc[@"ModInfo"][@"unofficial"]    = @"true";
                            if (int.TryParse(thirdPartyInfo.updatecode, out var updatecode) && updatecode > 0)
                            {
                                virtualModDesc[@"ModInfo"][@"updatecode"] = updatecode.ToString();
                                virtualModDesc[@"ModInfo"][@"modver"]     = 0.001.ToString(CultureInfo.InvariantCulture); //This will force mod to check for update after reload
                            }
                            else
                            {
                                virtualModDesc[@"ModInfo"][@"modver"] = 0.0.ToString(CultureInfo.InvariantCulture); //Will attempt to look up later after mods have parsed.
                            }

                            virtualModDesc[@"CUSTOMDLC"][@"sourcedirs"]        = dlcFolderName;
                            virtualModDesc[@"CUSTOMDLC"][@"destdirs"]          = dlcFolderName;
                            virtualModDesc[@"UPDATES"][@"originalarchivehash"] = md5;

                            var archiveSize    = new FileInfo(archive.FileName).Length;
                            var importingInfos = ThirdPartyServices.GetImportingInfosBySize(archiveSize);
                            if (importingInfos.Count == 1 && importingInfos[0].GetParsedRequiredDLC().Count > 0)
                            {
                                OnlineContent.QueryModRelay(importingInfos[0].md5, archiveSize); //Tell telemetry relay we are accessing the TPIS for an existing item so it can update latest for tracking
                                virtualModDesc[@"ModInfo"][@"requireddlc"] = importingInfos[0].requireddlc;
                            }

                            return(new Mod(virtualModDesc.ToString(), FilesystemInterposer.DirectoryGetParent(dlcDir, true), archive));
                        }

                        //Mod is marked for preventing import
                        return(new Mod(false)
                        {
                            ModName = thirdPartyInfo.modname,
                            ModDeveloper = thirdPartyInfo.moddev,
                            LoadFailedReason = M3L.GetString(M3L.string_modCannotBeImportedDueToOneOfTheFollowingReasons)
                        });
                    }
                    else
                    {
                        Log.Information($@"No third party mod information for importing {dlcFolderName}. Should this be supported for import? Contact Mgamerz on the ME3Tweaks Discord if it should.");
                    }
                }
            }
            return(null);
        }
Пример #36
0
        private static IEnumerable <FileData> EnumerateFiles(Stream input, string fileName,
                                                             FileFilter fileFilter, bool checkEncoding, List <string> includeSearchPatterns,
                                                             List <Regex> includeRegexPatterns, List <Regex> excludeRegexPatterns,
                                                             List <Regex> includeShebangPatterns, HashSet <string> hiddenDirectories)
        {
            using (SevenZipExtractor extractor = new SevenZipExtractor(input, true))
            {
                foreach (var fileInfo in extractor.ArchiveFileData)
                {
                    FileData fileData = new FileData(fileName, fileInfo);

                    var    attr          = (FileAttributes)fileInfo.Attributes;
                    string innerFileName = fileInfo.FileName;

                    int index = fileInfo.Index;
                    if (innerFileName == "[no name]" && extractor.ArchiveFileData.Count == 1)
                    {
                        index         = 0;
                        innerFileName = Path.GetFileNameWithoutExtension(fileName);
                        ArchiveFileInfo temp = Copy(fileInfo);
                        temp.FileName = innerFileName;
                        fileData      = new FileData(fileName, temp);
                    }

                    if (fileInfo.IsDirectory)
                    {
                        if (!fileFilter.IncludeHidden && attr.HasFlag(FileAttributes.Hidden) &&
                            !hiddenDirectories.Contains(innerFileName))
                        {
                            hiddenDirectories.Add(innerFileName + Path.DirectorySeparator);
                        }

                        continue;
                    }

                    if (!fileFilter.IncludeHidden)
                    {
                        if (attr.HasFlag(FileAttributes.Hidden))
                        {
                            continue;
                        }

                        bool excludeFile = false;
                        foreach (string dir in hiddenDirectories)
                        {
                            if (innerFileName.StartsWith(dir))
                            {
                                excludeFile = true;
                                break;
                            }
                        }

                        if (excludeFile)
                        {
                            continue;
                        }
                    }

                    if (Utils.IsArchive(innerFileName))
                    {
                        using (Stream stream = new MemoryStream())
                        {
                            extractor.ExtractFile(index, stream);

                            var enumerator = EnumerateFiles(stream, fileName + ArchiveSeparator + innerFileName,
                                                            fileFilter, checkEncoding, includeSearchPatterns, includeRegexPatterns,
                                                            excludeRegexPatterns, includeShebangPatterns, hiddenDirectories).GetEnumerator();

                            while (true)
                            {
                                FileData ret = null;
                                try
                                {
                                    if (!enumerator.MoveNext())
                                    {
                                        break;
                                    }
                                    ret = enumerator.Current;
                                }
                                catch (Exception ex)
                                {
                                    string msg = string.Format(CultureInfo.CurrentCulture, "Failed to search inside archive '{0}'", fileName + ArchiveSeparator + innerFileName);
                                    logger.Error(ex, msg);

                                    fileData.ErrorMsg = msg + ": " + ex.Message;
                                    ret = fileData;
                                }
                                if (ret != null)
                                {
                                    yield return(ret);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (IncludeFile(innerFileName,
                                        fileName + ArchiveSeparator + innerFileName,
                                        fileFilter, fileData, includeSearchPatterns,
                                        includeRegexPatterns, excludeRegexPatterns))
                        {
                            if (NeedsIncludeFileStream(fileName, fileFilter, checkEncoding,
                                                       includeSearchPatterns, includeShebangPatterns))
                            {
                                using (Stream stream = new MemoryStream(4096))
                                {
                                    extractor.ExtractFile(index, stream);

                                    if (IncludeFileStream(stream, fileFilter, fileData,
                                                          checkEncoding, includeShebangPatterns))
                                    {
                                        yield return(fileData);
                                    }
                                }
                            }
                            else
                            {
                                yield return(fileData);
                            }
                        }
                    }

                    if (Utils.CancelSearch)
                    {
                        break;
                    }
                }
            }
        }
Пример #37
0
 public bool DeleteFile(ArchiveFileInfo afi) => false;
Пример #38
0
 private ToolStripItem[] GetArchiveMenus(TreeNode node, ArchiveFileInfo info)
 {
     return(info.FileWrapper.GetContextMenuItems());
 }
Пример #39
0
        private Stream ExtractEntry(SevenZipExtractor zip, ArchiveFileInfo? entry)
        {
            if (entry != null && zip != null)
            {
                var memoryStream = new MemoryStream();

                ArchiveFileInfo entryValue = entry.GetValueOrDefault();

                if (entryValue != null)
                {
                    zip.ExtractFile(entryValue.Index, memoryStream);
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    return memoryStream;
                }
            }
            return null;
        }
Пример #40
0
        /// <summary>
        /// Create UploadPartRequest objects for a multipart upload.
        /// </summary>
        /// <param name="isNewFileUpload"></param>
        /// <param name="existingBucketName"></param>
        /// <param name="fileInfo"></param>
        /// <param name="uploadedParts"></param>
        /// <param name="partsProgress"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public Queue <UploadPartRequest> PreparePartRequests(bool isNewFileUpload, string existingBucketName, ArchiveFileInfo fileInfo,
                                                             List <PartDetail> uploadedParts, Dictionary <int, long> partsProgress)
        {
            long filePosition = 0;
            long partSize     = PART_SIZE; // 5 MB
            Queue <UploadPartRequest> partRequests = new Queue <UploadPartRequest>();

            if (fileInfo.Size < partSize)
            {
                partSize = fileInfo.Size;
            }

            for (int i = 1; filePosition < fileInfo.Size; i++)
            {
                var uploadedPart = uploadedParts.Where(x => x.PartNumber == i).FirstOrDefault();
                if (uploadedPart != null) // uploadedParts.Select(x => x.PartNumber).Contains(i))
                {
                    // for each already uploaded part, add total part size as transferred bytes.
                    partsProgress.Add(i, uploadedPart.Size);
                    // move the file position
                    filePosition += uploadedPart.Size;
                    continue;
                }

                // for each NOT uploaded part, add a pair in the progress dictionary with value = 0
                partsProgress.Add(i, 0);

                bool isLastPart = false;

                // check remaining size and if it's smalled than partSize
                // then set partSize = remainingSize and this as the last part in its request.
                long remainingSize = fileInfo.Size - filePosition;
                if (remainingSize <= partSize)
                {
                    isLastPart = true;
                    partSize   = remainingSize;
                }

                // Create request to upload a part.
                var uploadPartRequest =
                    CreateUploadPartRequest(existingBucketName, fileInfo.KeyName, fileInfo.UploadId, i, partSize, filePosition, fileInfo.FilePath);

                // Now check if this is the last part and mark the request.
                if (isLastPart)
                {
                    uploadPartRequest.IsLastPart = true;
                }

                partRequests.Enqueue(uploadPartRequest);

                // increment position by partSize
                filePosition += partSize;
            }

            return(partRequests);
        }
Пример #41
0
        public async Task <bool> UploadMultipartFileAsync(bool isNewFileUpload, string existingBucketName, ArchiveFileInfo fileInfo,
                                                          CancellationTokenSource cts, CancellationToken token, IProgress <Tuple <string, long> > progress)
        {
            token.ThrowIfCancellationRequested();

            _log.Debug("Called UploadMultipartFileAsync with ArchiveFileInfo properties: KeyName = \"" + fileInfo.KeyName +
                       "\", FilePath = \"" + fileInfo.FilePath + "\".");

            bool hasException = false;

            this.IsUploading = true;

            List <PartDetail>      uploadedParts           = new List <PartDetail>();
            Dictionary <int, long> multipartUploadProgress = new Dictionary <int, long>();

            var uploadPartTasks = new List <Task <UploadPartResponse> >();
            Queue <UploadPartRequest>         partRequests = new Queue <UploadPartRequest>();
            List <Task <UploadPartResponse> > runningTasks = new List <Task <UploadPartResponse> >();

            try
            {
                token.ThrowIfCancellationRequested();

                // if this isn't a new file upload (resuming a past upload)
                // then we have to get the uploaded parts so the upload continues
                // where it's stopped.
                if (!isNewFileUpload)
                {
                    uploadedParts = await this.ListPartsAsync(existingBucketName, fileInfo.KeyName, fileInfo.UploadId, token).ConfigureAwait(false);
                }

                token.ThrowIfCancellationRequested();

                // create all part requests.
                partRequests = this.PreparePartRequests(isNewFileUpload, existingBucketName, fileInfo, uploadedParts, multipartUploadProgress);

                token.ThrowIfCancellationRequested();

                var parallelLimit = (MAX_PARALLEL_ALLOWED > 1) ? MAX_PARALLEL_ALLOWED : 2;

                // initialize first tasks to run.
                while (runningTasks.Count < parallelLimit && partRequests.Count > 0)
                {
                    var uploadTask = this.UploadPartAsync(partRequests.Dequeue(), multipartUploadProgress, token, progress);
                    runningTasks.Add(uploadTask);
                }

                while (runningTasks.Count > 0)
                {
                    token.ThrowIfCancellationRequested();

                    var finishedTask = await Task <UploadPartResponse> .WhenAny(runningTasks).ConfigureAwait(false);

                    runningTasks.Remove(finishedTask);

                    if (finishedTask.Status == TaskStatus.Faulted)
                    {
                        throw finishedTask.Exception;
                    }

                    if (partRequests.Count > 0)
                    {
                        token.ThrowIfCancellationRequested();

                        var uploadTask = this.UploadPartAsync(partRequests.Dequeue(), multipartUploadProgress, token, progress);
                        runningTasks.Add(uploadTask);
                    }

                    finishedTask = null;
                }

                token.ThrowIfCancellationRequested();

                // if all goes well, return true
                return(true);
            }
            catch (Exception)
            {
                hasException = true;

                partRequests.Clear();
                runningTasks.Clear();
                multipartUploadProgress.Clear();
                uploadedParts.Clear();
                uploadPartTasks.Clear();

                throw;
            }
            finally
            {
                if (hasException && cts != null && !cts.IsCancellationRequested)
                {
                    cts.Cancel();
                }

                uploadedParts.Clear();
                multipartUploadProgress.Clear();
                uploadPartTasks.Clear();
                partRequests.Clear();
                runningTasks.Clear();

                this.IsUploading = false;
            }
        }
Пример #42
0
        public bool AddFile(ArchiveFileInfo archiveFileInfo)
        {
            //First we need to determine the paths
            string fullPath      = archiveFileInfo.FileName.Replace("\\", "/");
            string filePath      = Path.GetFileName(fullPath);
            string filePathNoExt = Path.GetFileNameWithoutExtension(fullPath);
            string directoryPath = Path.GetDirectoryName(fullPath).Replace("\\", "/");

            ulong fullPathHash  = 0;
            ulong directoryHash = 0;
            ulong fileHash      = 0;

            //Calculate hashes for each one

            //Check for full path hashes
            if (fullPath.Contains("[FullHash="))
            {
                string HashString = fullPath.Split('=').LastOrDefault().Replace("]", string.Empty);
                HashString = Path.GetFileNameWithoutExtension(HashString);

                filePath = filePath.Split('[').FirstOrDefault();

                TryParseHex(HashString, out fullPathHash);
            }

            ulong hash            = 0;
            bool  isDirectoryHash = TryParseHex(directoryPath, out hash);
            bool  isFileHash      = TryParseHex(filePath, out hash);

            if (isFileHash)
            {
                TryParseHex(filePath, out fileHash);
            }
            else
            {
                fileHash = FNV64A1.Calculate(filePath);
            }

            if (isDirectoryHash)
            {
                TryParseHex(directoryPath, out directoryHash);
            }
            else
            {
                directoryHash = FNV64A1.Calculate($"{directoryPath}/");
            }

            if (!isFileHash && !isDirectoryHash)
            {
                fullPathHash = FNV64A1.Calculate(fullPath);
            }

            var folder = folders.FirstOrDefault(x => x.hash == directoryHash);

            Console.WriteLine($"{fullPath} FilePathHash {fullPathHash}");
            Console.WriteLine($"{directoryPath} FolderHash {directoryHash} directoryHash {directoryHash}");
            Console.WriteLine($"{filePath} fileHash {fileHash} isFileHash {isFileHash}");

            if (folder != null)
            {
                folder.hashes.Add(new HashIndex()
                {
                    hash   = fileHash,
                    Parent = folder,
                    Index  = files.Count,
                });
            }
            else
            {
                folder      = new Folder();
                folder.hash = directoryHash;
                folder.hashes.Add(new HashIndex()
                {
                    hash   = fileHash,
                    Parent = folder,
                    Index  = files.Count,
                });
                folders.Add(folder);
            }

            files.Add(new FileEntry(this)
            {
                FilePathHash   = fullPathHash,
                FolderHash     = folder.hashes.LastOrDefault(),
                FileData       = archiveFileInfo.FileData,
                FileDataStream = archiveFileInfo.FileDataStream,
                FileName       = archiveFileInfo.FileName,
            });

            return(true);
        }
Пример #43
0
 /// <summary>
 /// Initializes a new instance of the FileInfoEventArgs class.
 /// </summary>
 /// <param name="fileInfo">The current ArchiveFileInfo.</param>
 /// <param name="percentDone">The percent of finished work.</param>
 public FileInfoEventArgs(ArchiveFileInfo fileInfo, byte percentDone)
     : base(percentDone)
 {
     _fileInfo = fileInfo;
 }
Пример #44
0
 /// <inheritdoc />
 public Task <LoadResult> LoadFile(IStateInfo stateInfo, ArchiveFileInfo afi, LoadFileContext loadFileContext)
 {
     return(LoadFile(stateInfo, afi, Guid.Empty, loadFileContext));
 }
Пример #45
0
 /// <summary>
 /// Refreshes the information in this object with new data retrieved
 /// from an archive.
 /// </summary>
 /// <param name="newFileInfo">Fresh instance for the same file just
 /// read from the archive.</param>
 /// <remarks>
 /// This implementation refreshes the <see cref="CabinetFolderNumber"/>.
 /// </remarks>
 protected override void Refresh(ArchiveFileInfo newFileInfo)
 {
     base.Refresh(newFileInfo);
     this.cabFolder = ((CabFileInfo) newFileInfo).cabFolder;
 }
 ArchiveInfo(ArchiveFileInfo archfileinfo)
 {
     afile = archfileinfo;
 }