public async Task <byte[]> ReadAsync() { using (var memory = new MemoryStream()) using (var stream = _archiveEntry.OpenEntryStream()) { await stream.CopyToAsync(memory); return(memory.ToArray()); } }
public override async Task <byte[]> ReadAsync() { if (_reader != null) { return(await Task.Run(() => _reader(_archiveEntry.Key)).ConfigureAwait(false)); } using (var memory = new MemoryStream()) using (var stream = _archiveEntry.OpenEntryStream()) { await stream.CopyToAsync(memory); return(memory.ToArray()); } }
public override Task <byte[]> ReadAsync() { if (_reader != null) { return(Task.Run(() => _reader(_archiveEntry.Key))); } return(Task.Run(() => { using (var memory = new MemoryStream()) using (var stream = _archiveEntry.OpenEntryStream()) { stream.CopyTo(memory); return memory.ToArray(); } })); }
public static void WriteTo(IArchiveEntry archiveEntry, Stream streamToWriteTo) { if ((archiveEntry.Archive.Type == ArchiveType.Rar) && archiveEntry.Archive.IsSolid) { throw new InvalidFormatException("Cannot use Archive random access on SOLID Rar files."); } if (archiveEntry.IsDirectory) { throw new ExtractionException("Entry is a file directory and cannot be extracted."); } IArchiveExtractionListener archive = archiveEntry.Archive as IArchiveExtractionListener; archive.EnsureEntriesLoaded(); archive.FireEntryExtractionBegin(archiveEntry); archive.FireFilePartExtractionBegin(archiveEntry.Key, archiveEntry.Size, archiveEntry.CompressedSize); Stream stream = archiveEntry.OpenEntryStream(); if (stream != null) { using (stream) { using (Stream stream2 = new ListeningStream(archive, stream)) { Utility.TransferTo(stream2, streamToWriteTo); } } archive.FireEntryExtractionEnd(archiveEntry); } }
public static string GetDescript(this IArchiveEntry archive) { using (var stream = new StreamReader(archive.OpenEntryStream())) { return(stream.ReadToEnd()); } }
public static void WriteTo(this IArchiveEntry archiveEntry, Stream streamToWriteTo) { if (archiveEntry.Archive.Type == ArchiveType.Rar && archiveEntry.Archive.IsSolid) { throw new InvalidFormatException("Cannot use Archive random access on SOLID Rar files."); } if (archiveEntry.IsDirectory) { throw new ExtractionException("Entry is a file directory and cannot be extracted."); } var streamListener = archiveEntry.Archive as IArchiveExtractionListener; streamListener.EnsureEntriesLoaded(); streamListener.FireEntryExtractionBegin(archiveEntry); streamListener.FireFilePartExtractionBegin(archiveEntry.Key, archiveEntry.Size, archiveEntry.CompressedSize); var entryStream = archiveEntry.OpenEntryStream(); if (entryStream == null) { return; } using (entryStream) using (Stream s = new ListeningStream(streamListener, entryStream)) { s.TransferTo(streamToWriteTo); } streamListener.FireEntryExtractionEnd(archiveEntry); }
public override Stream OpenStream(string identifier) { Func <IArchive> _archiveOpener = archiveOpener ?? throw new ObjectDisposedException(GetType().FullName); IArchive archive = _archiveOpener(); Stream s = null; IDisposable belate = null; try { // Search entry IArchiveEntry entry = archive.Entries.Where(e => e.Key == identifier).FirstOrDefault(); // Not found if (entry == null) { archive.Dispose(); return(null); } // Open stream s = entry.OpenEntryStream(); // Belate dispose of source belate = belateSource?.Belate(); Action belateCancel = belate == null ? null : (Action)(() => belate.Dispose()); // Attach the disposing of the archive to the stream. return(new StreamHandle(s, archive, belateCancel)); } catch (Exception) when(CloseDisposable(archive) || CloseDisposable(s) || CloseDisposable(belateSource)) { // Never goes here return(null); } }
public override Stream OpenStream(string identifier) { IArchive _archive = archive; SemaphoreSlim _lock = m_lock; CancellationTokenSource _cancelSrc = cancelSrc; if (_archive == null || _lock == null || _cancelSrc == null) { throw new ObjectDisposedException(GetType().FullName); } IArchiveEntry entry = _archive.Entries.Where(e => e.Key == identifier).FirstOrDefault(); if (entry == null) { return(null); } _lock.Wait(_cancelSrc.Token); Stream s = null; IDisposable belate = null; try { s = entry.OpenEntryStream(); belate = belateSource?.Belate(); StreamHandle sh = new StreamHandle(s, belate, () => { try { m_lock.Release(); } catch (Exception) { } }); return(sh); } catch (Exception) when(ReleaseSemaphore(_lock) || CloseDisposable(s) || CloseDisposable(belate)) { // Never goes here return(null); } }
public static void WriteTo(/*this*/ IArchiveEntry archiveEntry, Stream streamToWriteTo) { if (archiveEntry.Archive.Type == ArchiveType.Rar && archiveEntry.Archive.IsSolid) { throw new InvalidFormatException("Cannot use Archive random access on SOLID Rar files."); } if (archiveEntry.IsDirectory) { throw new ExtractionException("Entry is a file directory and cannot be extracted."); } var streamListener = archiveEntry.Archive as IArchiveExtractionListener; streamListener.EnsureEntriesLoaded(); streamListener.FireEntryExtractionBegin(archiveEntry); streamListener.FireFilePartExtractionBegin(archiveEntry.Key, archiveEntry.Size, archiveEntry.CompressedSize); var entryStream = archiveEntry.OpenEntryStream(); if (entryStream == null) { return; } using(entryStream) using (Stream s = new ListeningStream(streamListener, entryStream)) { //s.TransferTo(streamToWriteTo); Utility.TransferTo(s,streamToWriteTo); } streamListener.FireEntryExtractionEnd(archiveEntry); }
/// <summary> /// 从目标目录删除压缩文档中的文件列表 /// </summary> /// <param name="entry">压缩文档</param> /// <param name="destinationDirectory">目标目录</param> /// <param name="options">操作选项</param> private void DeleteFromDirectory(IArchiveEntry entry, string destinationDirectory, ExtractionOptions options) { using (Stream stream = entry.OpenEntryStream()) { using (StreamReader reader = new StreamReader(stream, this.Encoding)) { string file; while ((file = reader.ReadLine()) != null) { file = file.Trim(); if (file.Length <= 0) { continue; } file = Path.Combine(destinationDirectory, options.ExtractFullPath ? file : Path.GetFileName(file)); if (Directory.Exists(file)) { Directory.Delete(file, true); } else { File.Delete(file); } } } } }
// 通过压缩流创建图片 public static Image Create(IArchiveEntry entry) { try { if (entry.IsEncrypted) { return(null); } using var stream = entry.OpenEntryStream(); using var ms = new MemoryStream(); stream.CopyTo(ms); // bytes = ms.ToArray(); //return Image.FromStream(ms); //var format = Image.DetectFormat(ms); //Console.WriteLine(format); // return Image.Load(ms); return(Image.Load(ms.ToArray())); } catch (Exception e) { Console.WriteLine("创建图片出现异常:"); Console.WriteLine(e); } return(null); }
public static void WriteTo(this IArchiveEntry archiveEntry, Stream streamToWriteTo) { if (archiveEntry.IsDirectory) { throw new ExtractionException("Entry is a file directory and cannot be extracted."); } var streamListener = (IArchiveExtractionListener)archiveEntry.Archive; streamListener.EnsureEntriesLoaded(); streamListener.FireEntryExtractionBegin(archiveEntry); streamListener.FireFilePartExtractionBegin(archiveEntry.Key, archiveEntry.Size, archiveEntry.CompressedSize); var entryStream = archiveEntry.OpenEntryStream(); if (entryStream is null) { return; } using (entryStream) { using (Stream s = new ListeningStream(streamListener, entryStream)) { s.TransferTo(streamToWriteTo); } } streamListener.FireEntryExtractionEnd(archiveEntry); }
private async Task <EntryUnpackResult> UnpackEntryAsync(IArchiveEntry entry, string directory) { var ext = Path.GetExtension(entry.Key); if (entry.Key.ToLower().EndsWith(".srt.txt")) { ext = ".srt"; } var subtitleFound = this.subtitleExtensions.Contains(ext?.ToLower()); var targetFile = Path.Combine(directory, Path.ChangeExtension(entry.Key.Replace("?", ""), ext)); var dir = new FileInfo(targetFile).Directory; if (dir != null && !dir.Exists) { dir.Create(); } var targetFileInfo = new FileInfo(targetFile); using (var entryStream = entry.OpenEntryStream()) using (var sw = targetFileInfo.Create())//new FileStream(targetFile, FileMode.Create)) { var read = 0; var buffer = new byte[4096]; while ((read = await entryStream.ReadAsync(buffer, 0, buffer.Length)) != 0) { await sw.WriteAsync(buffer, 0, read); } return(new EntryUnpackResult(subtitleFound: subtitleFound, filename: targetFile, entry: entry.Key)); } }
public async Task <BitmapImage> GenerateBitmapImageAsync(CancellationToken ct) { using (var entryStream = _entry.OpenEntryStream()) using (var memoryStream = _recyclableMemoryStreamManager.GetStream()) { entryStream.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); ct.ThrowIfCancellationRequested(); var bitmapImage = new BitmapImage(); await bitmapImage.SetSourceAsync(memoryStream.AsRandomAccessStream()).AsTask(ct); ct.ThrowIfCancellationRequested(); return(bitmapImage); } }
/// <summary> /// Uncompresses the <see cref="IArchiveEntry"/> into a <see cref="MemoryStream"/>. /// </summary> /// <param name="archiveEntry">The <see cref="IArchiveEntry"/> to uncompress into a <see cref="MemoryStream"/>.</param> /// <returns>The <see cref="MemoryStream"/> containing the uncompressed <see cref="IArchiveEntry"/>.</returns> public static MemoryStream ExtractToStream(this IArchiveEntry archiveEntry) { using var compressedStream = archiveEntry.OpenEntryStream(); var memoryStream = new MemoryStream(); compressedStream.CopyTo(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); return(memoryStream); }
private void ProcessEntry(IArchiveEntry entry, IList <Stream> imageStreams, ZipArchive output, SimpleEncoder encoder) { using (var stream = entry.OpenEntryStream()) { var ms = new MemoryStream(); imageStreams.Add(ms); if (entry.Key.StartsWith("__MACOSX")) { return; } if (!entry.Key.EndsWith(".jpg") && !entry.Key.EndsWith(".png")) { stream.CopyTo(ms); output.AddEntry(entry.Key, ms); return; } Bitmap bits; try { bits = new Bitmap(stream); } catch (Exception e) { Logger.LogError("Error parsing bitmap: " + entry.Key); Logger.LogDebug(e, LogLevel.Error); return; } if (bits.PixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb) { var newBits = ChangePixelFormat(bits); bits.Dispose(); bits = newBits; } try { encoder.Encode(bits, ms, Quality); output.AddEntry(entry.Key + ".webp", ms); } catch (Exception e) { Logger.LogError("Error encoding entry: " + entry.Key); Logger.LogDebug(e, LogLevel.Error); } finally { bits.Dispose(); } } }
/// <summary> /// Opens an file for reading /// </summary> /// <param name="file">The name of the file to open</param> /// <returns>A stream with the file contents</returns> public Stream OpenRead(string file) { if (m_isWriting) { throw new InvalidOperationException("Cannot read while writing"); } IArchiveEntry ze = GetEntry(file); return(ze == null ? null : ze.OpenEntryStream()); }
private Bitmap loadAndResizeBitmap(IArchiveEntry fileEntry) { Bitmap bitmap = (Bitmap)Bitmap.FromStream(fileEntry.OpenEntryStream()); if (bitmap.Height > System.Windows.SystemParameters.PrimaryScreenHeight) { System.Drawing.Size oldSize = new System.Drawing.Size(bitmap.Width, bitmap.Height); System.Drawing.Size newSize = getNewImageSize(oldSize); Bitmap newImage = ResizeImage(bitmap, newSize); bitmap = newImage; } return(bitmap); }
/// <summary> /// Create file provider that re-opens archive. /// </summary> /// <param name="archiveOpener"></param> /// <param name="entryName">Entry name of the whole package</param> /// <param name="hintPath">(optional) archive name "folder/document.txt.gz", entry name is extracted by removing the folder (separator '/') and last extension.</param> /// <param name="dateTime">Date time for folder entries</param> /// <exception cref="IOException">On I/O error</exception> /// <exception cref="PackageException.LoadError">on file format error</exception> public GZipFileProvider(Func <GZipArchive> archiveOpener, string entryName, string hintPath = null, DateTimeOffset?dateTime = default) : base(hintPath, dateTime) { if (archiveOpener == null) { throw new ArgumentNullException(nameof(archiveOpener)); } // Place holder for the uncompressed length value long[] length = new long[1]; // Convert archiveOpener to streamOpener Stream streamOpener() { IArchive archive = archiveOpener(); try { // Search entry IArchiveEntry entry = archive.Entries.First(); // Not found if (entry == null) { archive.Dispose(); return(null); } // Open stream Stream s = entry.OpenEntryStream(); // Attach the disposing of the archive to the stream. return(new GZipStreamFix(s, archive, null, length[0])); } catch (Exception) when(CloseDisposable(archive)) { // Never goes here return(null); } } // Create stream provider this.streamProvider = new StreamOpener(streamOpener, entryName, belatedDisposeList); // Open once to read entries. using (var archive = archiveOpener()) { // Read first entry IArchiveEntry entry = archive.Entries.First(); // Get length using (var s = entry.OpenEntryStream()) length[0] = CalculateLength(s); // File entry var fileEntry = new Lexical.FileProvider.Common.ArchiveFileEntry(streamProvider, entryName, entryName, length[0], dateTime ?? entry.LastModifiedTime ?? DateTime.MinValue); // Put it in directory this.root.files[entryName] = fileEntry; } }
/// <summary> /// Create file provider that reads one entry from <paramref name="archive"/>. /// </summary> /// <param name="archive"></param> /// <param name="entryName">Entry name of the whole package</param> /// <param name="hintPath">(optional) archive name "folder/document.txt.gz", entry name is extracted by removing the folder (separator '/') and last extension.</param> /// <param name="dateTime">(optional) Date time for folder entries</param> public GZipFileProvider(GZipArchive archive, string entryName, string hintPath = null, DateTimeOffset?dateTime = null) : base(hintPath, dateTime) { this.streamProvider = new Lexical.FileProvider.SharpCompress.Internal.ArchiveStreamProvider(archive ?? throw new ArgumentNullException(nameof(archive)), belatedDisposeList); IArchiveEntry entry = archive.Entries.First(); long length; using (Stream s = entry.OpenEntryStream()) length = CalculateLength(s); var fileEntry = new Lexical.FileProvider.Common.ArchiveFileEntry(streamProvider, entry.Key, entryName, length, dateTime ?? entry.LastModifiedTime ?? DateTime.MinValue); this.root.files[entryName] = fileEntry; }
public override Stream OpenStream(string path) { IArchiveEntry entory = archive.Entries.First(e => e.Key == path); using (var rarStream = entory.OpenEntryStream()) { try { var ms = new MemoryStream(); rarStream.CopyTo(ms); ms.Position = 0; return(ms); } catch { return(Stream.Null); } } }
private static string GetFolderPath(DirectoryInfo output, string mainEntryKey, string currentEntryKey, IArchiveEntry archiveEntry) { using Stream jsonFileCopy = CreateCopy(archiveEntry.OpenEntryStream()); BdnResult bdnResult = Helper.ReadFromStream(jsonFileCopy); string userName = mainEntryKey.Split('/')[2]; // sth like Performance-Runs/nativeaot6.0/adsitnik/arm64_win10-nativeaot6.0.tar.gz string moniker = GetMoniker(currentEntryKey) ?? GetMoniker(mainEntryKey); StringBuilder sb = new StringBuilder(); sb.Append(bdnResult.HostEnvironmentInfo.Architecture).Append('_'); sb.Append(Stats.GetSimplifiedOSName(bdnResult.HostEnvironmentInfo.OsVersion).Replace(" ", "")).Append('_'); sb.Append(GetSimplifiedProcessorName(bdnResult.HostEnvironmentInfo.ProcessorName).Replace(" ", "")).Append('_'); sb.Append(userName).Append('_'); sb.Append(moniker); // netX-previewY string outputPath = Path.Combine(output.FullName, sb.ToString().ToLower()); Directory.CreateDirectory(outputPath); return(outputPath); }
// 将压缩流还原成图片的base64数据 public static string Read(IArchiveEntry entry) { try { if (entry.IsEncrypted) { return(null); } using var stream = entry.OpenEntryStream(); using var ms = new MemoryStream(); stream.CopyTo(ms); // bytes = ms.ToArray(); return("data:image/*;base64," + Convert.ToBase64String(ms.ToArray())); } catch (Exception e) { Console.WriteLine("创建图片出现异常:"); Console.WriteLine(e); } return(null); }
static string GetString(IArchiveEntry entry) { byte[] bytes = new byte[entry.Size]; entry.OpenEntryStream().Read(bytes, 0, bytes.Length); return(System.Text.Encoding.Default.GetString(bytes)); }
private Bitmap loadAndResizeBitmap(IArchiveEntry fileEntry) { Bitmap bitmap = (Bitmap)Bitmap.FromStream(fileEntry.OpenEntryStream()); if (bitmap.Height > System.Windows.SystemParameters.PrimaryScreenHeight) { System.Drawing.Size oldSize = new System.Drawing.Size(bitmap.Width, bitmap.Height); System.Drawing.Size newSize = getNewImageSize(oldSize); Bitmap newImage = ResizeImage(bitmap, newSize); bitmap = newImage; } return bitmap; }
static string GetString(IArchiveEntry entry) { byte[] bytes = new byte[entry.Size]; entry.OpenEntryStream().Read(bytes, 0, bytes.Length); return System.Text.Encoding.Default.GetString(bytes); }
private Bitmap LoadImage(IArchiveEntry fileEntry) { Bitmap bitmap = (Bitmap)Bitmap.FromStream(fileEntry.OpenEntryStream()); return(bitmap); }
private async Task ReadCollectionFromZip(IArchiveEntry entry, ZipDirectory currentDirectory, string currentPath, Dictionary<string, IArchiveEntry> resourceEntries, Indiagram parent) { using (Stream entryStream = entry.OpenEntryStream()) { XDocument xmlDocument = XDocument.Load(entryStream); XElement rootElement = xmlDocument.Element("indiagram"); if (rootElement != null) { // the current element is an indiagram, just read it await CreateIndiagramFromXml(rootElement, false, parent, async (key, type) => { return await Task.Run(() => { if (resourceEntries.ContainsKey(key)) { return resourceEntries[key].OpenEntryStream(); } throw new IndexOutOfRangeException(string.Format("Key {0} is not available in resources", key)); }); }); } else { // the current element is a category, read it + process its children rootElement = xmlDocument.Element("category"); if (rootElement == null) { return; } Indiagram category = await CreateIndiagramFromXml(rootElement, true, parent, async (key, type) => { return await Task.Run(() => { if (resourceEntries.ContainsKey(key)) { return resourceEntries[key].OpenEntryStream(); } throw new IndexOutOfRangeException(string.Format("Key {0} is not available in resources", key)); }); }); XElement indiagramsElement = rootElement.Element("indiagrams"); if (indiagramsElement == null) { return; } foreach (XElement child in indiagramsElement.Elements("indiagram")) { // look for the entry string directoryName = Path.GetDirectoryName(child.Value); string fileName = Path.GetFileName(child.Value); ZipDirectory directory = GetSubZipDirectory(currentDirectory, currentPath, directoryName); if (directory.Files.ContainsKey(fileName)) { await ReadCollectionFromZip(directory.Files[fileName], directory, directoryName, resourceEntries, category); } else { throw new IndexOutOfRangeException("file names mismatch"); } } } } }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "SevenZipV2/{account}/{container}/{directory}/{filename}")] HttpRequest req, string account, string container, string directory, string filename, ILogger log) { //Retrieve File from storage var lakeClient = SevenZip.GetDataLakeServiceClient(HttpUtility.UrlDecode(account)); var fileSystemClient = lakeClient.GetFileSystemClient(HttpUtility.UrlDecode(container)); DataLakeDirectoryClient directoryClient = fileSystemClient.GetDirectoryClient(HttpUtility.UrlDecode(directory)); var DownloadFile = directoryClient.GetFileClient(HttpUtility.UrlDecode(filename)); var ReadStream = await DownloadFile.OpenReadAsync(); //Begin to send first http response var response = req.HttpContext.Response; response.StatusCode = 200; response.ContentType = "application/json-data-stream"; await response.Body.WriteAsync(Encoding.UTF8.GetBytes("[")); using (var archive = SevenZipArchive.Open(ReadStream, null)) { //Retrieve uncompressed Stream IArchiveEntry e = archive.Entries.FirstOrDefault(); var un7ZipStream = e.OpenEntryStream(); log.LogInformation($"Stream Opened {filename}"); XmlReaderSettings settings = new XmlReaderSettings(); settings.Async = true; settings.IgnoreWhitespace = true; using (XmlReader reader = XmlReader.Create(un7ZipStream, settings)) { bool keepReading = reader.Read(); bool firstrow = true; while (keepReading) { if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "row") && reader.HasAttributes) { dynamic exo = new ExpandoObject(); for (int attInd = 0; attInd < reader.AttributeCount; attInd++) { reader.MoveToAttribute(attInd); ((IDictionary <String, Object>)exo).Add(reader.Name, reader.Value); /*if(reader.Name == "Id"&&int.Parse(reader.Value)%1000==0) * { * log.LogInformation(reader.Value); * } */ } await response.Body.WriteAsync(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(exo))); } if (reader.Read()) { if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "row") && reader.HasAttributes) { if (!firstrow) { await response.Body.WriteAsync(Encoding.UTF8.GetBytes(",")); } else { firstrow = false; } } } else { keepReading = false; } } } } await response.Body.WriteAsync(Encoding.UTF8.GetBytes("]")); return(new EmptyResult()); }
public CompressedROMFile(IArchiveEntry entry, FileInfo path) { this.entry = entry; archivePath = path; archiveStream = new WrappedInputStream(entry.OpenEntryStream()); }