public void Save(string path) { TagBuilder builder = new TagBuilder(majorVersion, minorVersion); foreach (FrameEntry frame in frames) { builder.Append(frame.frameId, frame.frameValue); } // TODO: Exception handler to clean up after any failures. // Create a temporary file: TemporaryFile tempFile = new TemporaryFile(path); FileStream tempStream = new FileStream(tempFile.Path, FileMode.Create, FileAccess.Write, FileShare.None); builder.WriteTo(tempStream); // Skip the tags in the source file. FileStream sourceStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None); long pos0 = sourceStream.Position; TagUtil.SkipTags(sourceStream); long pos1 = sourceStream.Position; StreamCopier.Copy(sourceStream, tempStream); sourceStream.Close(); tempStream.Close(); // Then we can swap the files over. tempFile.Swap(); }
/// <summary> /// Copy ID3v2 tags from one stream to another. /// </summary> /// <param name="source">The source stream. Assumed positioned at the 'ID3' header, i.e. the start of the file.</param> /// <param name="dest">The destination stream.</param> private static void CopyTags(Stream source, Stream dest) { // Figure out the length of the ID3 header in the source stream: long returnPos = source.Position; long tagEndOffset = TagUtil.GetTagEndOffset(source); source.Seek(returnPos, SeekOrigin.Begin); // Now copy that many bytes into the destination: StreamCopier.Copy(source, dest, tagEndOffset); }
public static void Copy(string source, string destination) { // TODO: Exception handler, or finally block, to clean up on failures? // We need to copy the tags from the source file to a temporary file. FileStream sourceStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read); sourceStream.Seek(0, SeekOrigin.Begin); // Create a temporary file: TemporaryFile tempFile = new TemporaryFile(destination); // Open it. FileStream tempStream = new FileStream(tempFile.Path, FileMode.Create, FileAccess.Write, FileShare.None); // Copy the tags. CopyTags(sourceStream, tempStream); // We don't need the source file any more. sourceStream.Close(); // Get the destination file open: FileStream destinationStream = new FileStream(destination, FileMode.Open, FileAccess.Read, FileShare.None); // Now we need to skip the tags in the destination. TagUtil.SkipTags(destinationStream); // Copy the remainder of the output file into the temporary file. StreamCopier.Copy(destinationStream, tempStream); // We don't need the destination file any more. destinationStream.Close(); // We don't need the temp file any more. tempStream.Close(); // Do the rename shuffle. tempFile.Swap(); }
public static void Remove(string path) { // TODO: Exception handler to clean up on failures. // Open the source file, and then skip over any tags. FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); // Go back to the beginning of the stream. stream.Seek(0, SeekOrigin.Begin); TagUtil.SkipTags(stream); // Create a temporary file: TemporaryFile tempFile = new TemporaryFile(path); FileStream dest = new FileStream(tempFile.Path, FileMode.Create, FileAccess.Write, FileShare.None); StreamCopier.Copy(stream, dest); stream.Close(); dest.Close(); tempFile.Swap(); }