private static void WriteContent(Stream fs, AssHeader header, Stream oldFileStream, Dictionary <string, string> fileRefs, List <AssParseInfo> piList) { var index = fs.Position; var endIndex = 0L; if (header.startPos == 0 && header.endPos == 0) { if (!fileRefs.ContainsKey(header.fileName) || !File.Exists(fileRefs[header.fileName])) { throw new ArgumentException("cannot find a source file to be archived, filename:" + header.fileName); } var filePath = fileRefs[header.fileName]; try { using (var f = File.OpenRead(filePath)) { endIndex = WriteToStream(f, fs); } } catch (Exception e) { throw new IOException("cannot read:" + filePath, e); } } else if (header.startPos < header.endPos) { throw new InvalidOperationException("AssHeader start position is greater than end position"); } else if (oldFileStream.Length < header.endPos) { throw new ArgumentOutOfRangeException("endPos", "AssHeader end position is out range from the old file stream"); } else if (!oldFileStream.CanSeek || !oldFileStream.CanRead) { throw new IOException("cant read or seek the olfFileStream"); } else { oldFileStream.Position = header.startPos; var len = header.endPos - header.startPos; endIndex = WriteToStream(oldFileStream, fs, len); } if (endIndex > index) { var pi = piList.First(_ => _.fileName == header.fileName); WriteBackPosition(fs, pi, index, endIndex); } else { throw new IOException("start index is greater than end index"); } }
private static AssParseInfo WriteHeader(Stream stream, AssHeader header) { stream.Write(header.fileName); var pi = new AssParseInfo { fileName = header.fileName, posIndex = stream.Position, }; stream.Write(0L); stream.Write(0L); return(pi); }
public void AddFile(string targetPath, string actualFilePath) { if (_headers.Any(_ => _.fileName == targetPath)) { throw new IOException("target file path already exists:" + targetPath); } if (!File.Exists(actualFilePath)) { throw new IOException("file not found:" + actualFilePath); } var s = new AssHeader { fileName = targetPath }; _headers.Add(s); _fileReference.Add(targetPath, actualFilePath); }