public void WriteFileTo(NZFileinfo file, Stream to, bool writeRawGzipData = false) { if (StreamProvider == null) { lock (streamlocker) { stream.Position = posOffset + file.pos; if (writeRawGzipData) { CopyStream(stream, to, file.ziplen); } else { using (var gz = new GZipStream(stream, CompressionMode.Decompress, true)) { CopyStream(gz, to, file.length); } } } } else { using (var stream = StreamProvider()) { stream.Position = posOffset + file.pos; if (writeRawGzipData) { CopyStream(stream, to, file.ziplen); } else { using (var gz = new GZipStream(stream, CompressionMode.Decompress, true)) { CopyStream(gz, to, file.length); } } } } }
public byte[] GetFileBytes(NZFileinfo fileInfo) { byte[] bytes = new byte[fileInfo.length]; var ms = new MemoryStream(bytes); WriteFileTo(fileInfo, ms, false); return(bytes); }
public NZFileinfo[] GetFiles() { var files = new NZFileinfo[header.Files.Length]; for (int i = 0; i < header.Files.Length; i++) { files[i] = header.Files[i]; } return(files); }
public static void Create(Stream output, IEnumerable <AddingFile> filesToAdd, TextWriter debugoutput = null) { var header = new PkgHeader(); header.NZipMinVersion = 1; header.NZipVersion = 2; var files = new List <NZFileinfo>(); var beginPosition = output.Position; output.Write(BitConverter.GetBytes(-1), 0, sizeof(int)); int lengthsum = (int)output.Position; int i = 0; foreach (var x in filesToAdd) { var nfi = new NZFileinfo(); nfi.name = x.name; debugoutput?.WriteLine("[{1}/{2}] \"{0}\"", nfi.name, i + 1, (filesToAdd as ICollection)?.Count.ToString() ?? "NaN"); #if NET45 using (var gz = new GZipStream(output, CompressionLevel.Optimal, true)) { #else using (var gz = new GZipStream(output, CompressionMode.Compress, true)) { #endif using (var file = x.file()) { nfi.length = file.Length; CopyStream(file, gz); } } nfi.pos = lengthsum; var curPos = (int)output.Position; nfi.ziplen = curPos - lengthsum; nfi.lwt = ConvertDateTimeInt(x.lwt); files.Add(nfi); lengthsum = curPos; i++; } header.Files = files.ToArray(); header.CreateTime = ConvertDateTimeInt(DateTime.UtcNow); debugoutput?.WriteLine("[Finishing] Writing PkgInfo..."); using (var ms = new MemoryStream(4 * 1024)) { using (var gz = new GZipStream(ms, CompressionMode.Compress, true)) { SerializePkgHeader(header, gz); } ms.WriteTo(output); } output.Position = beginPosition; output.Write(BitConverter.GetBytes(lengthsum), 0, sizeof(int)); debugoutput?.WriteLine("Finished!"); }
public async Task WriteFileToAsync(NZFileinfo file, Stream to, bool writeRawGzipData = false) { if (StreamProvider == null) { throw new Exception("async is not suppoted without StreamProvider."); } else { using (var stream = StreamProvider()) { stream.Position = posOffset + file.pos; if (writeRawGzipData) { await CopyStreamAsync(stream, to, file.ziplen); } else { using (var gz = new GZipStream(stream, CompressionMode.Decompress, true)) { await CopyStreamAsync(gz, to, file.length); } } } } }