public DebReader(string path) { bytes = File.ReadAllBytes(path); signature = bytes.Read(0, 8).ConvertToString(); debianBinary = new InnerFile(bytes, 8); control = new InnerFile(bytes, 8 + debianBinary.Length); data = new InnerFile(bytes, 8 + debianBinary.Length + control.Length); }
public static async Task ExportLogAsync(StorageFile ExportFile) { try { if (await ApplicationData.Current.TemporaryFolder.TryGetItemAsync(UniqueName) is StorageFile InnerFile) { await InnerFile.CopyAndReplaceAsync(ExportFile); } } catch (Exception ex) { Debug.WriteLine($"An error was threw in {nameof(ExportLogAsync)}, message: {ex.Message}"); } }
private void DecompressArchive( string outFolder, InnerFile innerFile, bool overwrite = false, Action <FileInfo> onFileDecompressed = null) { Directory.CreateDirectory(outFolder); using var memoryStream = new MemoryStream(innerFile.Content); using var reader = ReaderFactory.Open(memoryStream); while (reader.MoveToNextEntry()) { var outFile = new FileInfo(outFolder + Path.DirectorySeparatorChar + reader.Entry.Key); var wasExtracted = reader.WriteEntryToDirectoryWithFeedback(outFolder, new ExtractionOptions { WriteSymbolicLink = (symlinkName, destination) => { var symlink = new FileInfo(symlinkName); var origFile = new FileInfo(symlink.Directory.FullName + Path.DirectorySeparatorChar + destination); if (symlink.Exists) { symlink.Delete(); } origFile.CreateSymbolicLink(symlinkName, false); }, Overwrite = overwrite, ExtractFullPath = true, PreserveFileTime = true }); if (!overwrite && !wasExtracted && outFile.Exists) { Logger.Debug($"Skipping extraction of '{outFile.Name}' because the file already exists and overwrite is false."); } if (wasExtracted) { onFileDecompressed?.Invoke(outFile); } } }
private static async Task TarFolderCore(FileSystemStorageFolder Folder, TarOutputStream OutputStream, string BaseFolderName, ByteReadChangedEventHandler ByteReadHandler = null) { IReadOnlyList <FileSystemStorageItemBase> ItemList = await Folder.GetChildItemsAsync(true, true).ConfigureAwait(false); if (ItemList.Count == 0) { if (!string.IsNullOrEmpty(BaseFolderName)) { TarEntry NewEntry = TarEntry.CreateTarEntry($"{BaseFolderName}/"); OutputStream.PutNextEntry(NewEntry); OutputStream.CloseEntry(); } } else { ulong CurrentPosition = 0; foreach (FileSystemStorageItemBase Item in ItemList) { switch (Item) { case FileSystemStorageFolder InnerFolder: { ulong InnerFolderSize = 0; await TarFolderCore(InnerFolder, OutputStream, $"{BaseFolderName}/{InnerFolder.Name}", ByteReadHandler : (ByteRead) => { InnerFolderSize = ByteRead; ByteReadHandler?.Invoke(CurrentPosition + ByteRead); }).ConfigureAwait(false); ByteReadHandler?.Invoke(CurrentPosition += InnerFolderSize); break; } case FileSystemStorageFile InnerFile: { using (FileStream FileStream = await InnerFile.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false)) { TarEntry NewEntry = TarEntry.CreateTarEntry($"{BaseFolderName}/{InnerFile.Name}"); NewEntry.ModTime = DateTime.Now; NewEntry.Size = FileStream.Length; OutputStream.PutNextEntry(NewEntry); await FileStream.CopyToAsync(OutputStream, ProgressHandler : (s, e) => { ByteReadHandler?.Invoke(CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * InnerFile.SizeRaw)); }).ConfigureAwait(false); } OutputStream.CloseEntry(); ByteReadHandler?.Invoke(CurrentPosition += InnerFile.SizeRaw); break; } } } } }
private static async Task ZipFolderCore(FileSystemStorageFolder Folder, ZipOutputStream OutputStream, string BaseFolderName, CompressionAlgorithm Algorithm, ByteReadChangedEventHandler ByteReadHandler = null) { IReadOnlyList <FileSystemStorageItemBase> ItemList = await Folder.GetChildItemsAsync(true, true).ConfigureAwait(false); if (ItemList.Count == 0) { if (!string.IsNullOrEmpty(BaseFolderName)) { ZipEntry NewEntry = new ZipEntry($"{BaseFolderName}/") { DateTime = DateTime.Now }; OutputStream.PutNextEntry(NewEntry); OutputStream.CloseEntry(); } } else { ulong CurrentPosition = 0; foreach (FileSystemStorageItemBase Item in ItemList) { switch (Item) { case FileSystemStorageFolder InnerFolder: { ulong InnerFolderSize = 0; await ZipFolderCore(InnerFolder, OutputStream, $"{BaseFolderName}/{Item.Name}", Algorithm, (ByteRead) => { InnerFolderSize = ByteRead; ByteReadHandler?.Invoke(CurrentPosition + ByteRead); }).ConfigureAwait(false); ByteReadHandler?.Invoke(CurrentPosition += InnerFolderSize); break; } case FileSystemStorageFile InnerFile: { using (FileStream FileStream = await InnerFile.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false)) { ZipEntry NewEntry = new ZipEntry($"{BaseFolderName}/{Item.Name}") { DateTime = DateTime.Now, CompressionMethod = Algorithm == CompressionAlgorithm.None ? CompressionMethod.Stored : Enum.Parse <CompressionMethod>(Enum.GetName(typeof(CompressionAlgorithm), Algorithm)), Size = FileStream.Length }; OutputStream.PutNextEntry(NewEntry); await FileStream.CopyToAsync(OutputStream, ProgressHandler : (s, e) => { ByteReadHandler?.Invoke(CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * InnerFile.SizeRaw)); }).ConfigureAwait(false); } OutputStream.CloseEntry(); ByteReadHandler?.Invoke(CurrentPosition += Item.SizeRaw); break; } } } } }
private static async Task ZipFolderCore(FileSystemStorageFolder Folder, ZipOutputStream OutputStream, string BaseFolderName, ProgressChangedEventHandler ProgressHandler = null) { List <FileSystemStorageItemBase> ItemList = await Folder.GetChildItemsAsync(true).ConfigureAwait(false); if (ItemList.Count == 0) { if (!string.IsNullOrEmpty(BaseFolderName)) { ZipEntry NewEntry = new ZipEntry($"{BaseFolderName}/"); OutputStream.PutNextEntry(NewEntry); OutputStream.CloseEntry(); } } else { ulong TotalSize = await Folder.GetFolderSizeAsync().ConfigureAwait(false); ulong CurrentPosition = 0; foreach (FileSystemStorageItemBase Item in ItemList) { switch (Item) { case FileSystemStorageFolder InnerFolder: { ulong InnerFolderSize = await InnerFolder.GetFolderSizeAsync().ConfigureAwait(false); await ZipFolderCore(InnerFolder, OutputStream, $"{BaseFolderName}/{Item.Name}", ProgressHandler : (s, e) => { if (TotalSize > 0) { ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32((CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * InnerFolderSize)) * 100d / TotalSize), null)); } }).ConfigureAwait(false); if (TotalSize > 0) { CurrentPosition += InnerFolderSize; ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32(CurrentPosition * 100d / TotalSize), null)); } break; } case FileSystemStorageFile InnerFile: { using (FileStream FileStream = await InnerFile.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false)) { ZipEntry NewEntry = new ZipEntry($"{BaseFolderName}/{Item.Name}") { DateTime = DateTime.Now, CompressionMethod = CompressionMethod.Deflated, Size = FileStream.Length }; OutputStream.PutNextEntry(NewEntry); await FileStream.CopyToAsync(OutputStream, (s, e) => { ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32((CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * InnerFile.SizeRaw)) * 100d / TotalSize), null)); }).ConfigureAwait(false); } if (TotalSize > 0) { CurrentPosition += Item.SizeRaw; ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32(CurrentPosition * 100d / TotalSize), null)); } break; } } } } }
/// <summary> /// ファイル内の内部ファイルからバイナリデータを作成し、InnerFileDataに格納する。 /// </summary> private async void CreateInnerFileData() { this._createInnerFileData = Task.Run(() => { lock (this.InnerFileData) { if (this.SelectedInnerFileIndex < 0) { //何も選択されていない場合、ファイルデータを空にする。 this.InnerFileData = new InnerFile(InnerFile.FileType.None, null); return; } var fileInfo = this.InnerFileList[this.SelectedInnerFileIndex]; byte[] buff; //InnerFileData作成 try { using (var fs = File.OpenRead(Path.Combine(this.DirectoryPath, this.SelectedFileName))) using (var br = new BinaryReader(fs)) { fs.Seek(fileInfo.Offset, SeekOrigin.Begin); buff = br.ReadBytes((int)fileInfo.Zsize); if (fileInfo.Zsize != fileInfo.Size) { var ipfd = new IpfDecrypter(); buff = ipfd.Decrypt(buff); buff = new Func <byte[], byte[]>(source => { var result = new byte[fileInfo.Size]; using (var ms = new MemoryStream(source)) using (var ds = new DeflateStream(ms, CompressionMode.Decompress)) { ds.Read(result, 0, (int)fileInfo.Size); ms.Close(); ds.Close(); } return(result); })(buff); } } } catch (IOException) { //ファイルロック中 this.InnerFileData = new InnerFile(InnerFile.FileType.None, null); return; } //FileType作成 InnerFile.FileType fileType; if (Regex.IsMatch(fileInfo.Name, @"\.(jpg|png|bmp|gif)$")) { fileType = InnerFile.FileType.Image; } else if (Regex.IsMatch(fileInfo.Name, @"\.(xml|lun|skn|effect|lua)$")) { fileType = InnerFile.FileType.Text; } else if (Regex.IsMatch(fileInfo.Name, @"\.(ttf)$")) { fileType = InnerFile.FileType.Font; } else { fileType = InnerFile.FileType.Other; } this.InnerFileData = new InnerFile(fileType, buff); } }); await this._createInnerFileData; }