public async Task ExtractAll(string path, CancellationToken token) { foreach (var fileHeader in _cdHeaders) { if (fileHeader.CompressedSize != fileHeader.UncompressedSize) { throw new ArgumentException("Unable to extract, file is compressed!"); } _stream.Seek(fileHeader.LocalHeaderOffset + 516, SeekOrigin.Begin); var locFileHeaderSpan = new byte[LocalFileHeader.StaticHeaderSize + fileHeader.FilenameLength]; await _stream.ReadAsync(locFileHeaderSpan, token); var locFileHeader = new LocalFileHeader(locFileHeaderSpan); var fileData = new byte[locFileHeader.UncompressedSize]; await _stream.ReadAsync(fileData, token); var file = new PkZipFile(locFileHeader, fileHeader.LocalHeaderOffset, fileData); var filename = file.Filename; var filePath = Path.Combine(path, filename); if (!Regex.Match(filename, @"^(?!.*\.$)(?!.*?\.\.)[a-zA-Z0-9_.\\\/-]+$").Success) { throw new ArgumentException($"Invalid Filename: { filename }"); } Directory.CreateDirectory(Path.GetDirectoryName(filePath)); using (var stream = File.Create(filePath)) { await stream.WriteAsync(file.File, token); } } }
public void AddFile(PkZipFile file) { _files.Add(file); }