tags:

views:

1662

answers:

3

I can't seem to find a more efficient way to "copy" an embedded resource to disk, than the following:

using (BinaryReader reader = new BinaryReader(
    assembly.GetManifestResourceStream(@"Namespace.Resources.File.ext")))
{
    using (BinaryWriter writer
        = new BinaryWriter(new FileStream(path, FileMode.Create)))
    {
        long bytesLeft = reader.BaseStream.Length;
        while (bytesLeft > 0)
        {
            // 65535L is < Int32.MaxValue, so no need to test for overflow
            byte[] chunk = reader.ReadBytes((int)Math.Min(bytesLeft, 65536L));
            writer.Write(chunk);

            bytesLeft -= chunk.Length;
        }
    }
}

There appears to be no more direct way to do the copy, unless I'm missing something...

+2  A: 

Personally I would do it this way:

using (BinaryReader reader = new BinaryReader(
    assembly.GetManifestResourceStream(@"Namespace.Resources.File.ext")))
{
    using (BinaryWriter writer
        = new BinaryWriter(new FileStream(path, FileMode.Create)))
    {
        byte[] buffer = new byte[64 * 1024];
        int numread = reader.Read(buffer,0,buffer.Length);

        while (numread > 0)
        {
            writer.Write(buffer,0,numread);
            numread = reader.Read(buffer,0,buffer.Length);
        }

        writer.Flush();
    }
}
Lloyd
I like that minus the flush, but excluding a more direct way, I think I'm going to take yours as the answer.
sixlettervariables
+9  A: 

I'm not sure why you're using BinaryReader/BinaryWriter at all. Personally I'd start off with a useful utility method:

public static void CopyStream(Stream input, Stream output)
{
    // Insert null checking here for production
    byte[] buffer = new byte[8192];

    int bytesRead;
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}

then call it:

using (Stream input = assembly.GetManifestResourceStream(resourceName))
using (Stream output = File.Create(path))
{
    CopyStream(input, output);
}

You can change the buffer size of course, or have it as a parameter to the method - but the main point is that this is simpler code. Is it more efficient? Nope. Are you sure you really need this code to be more efficient? Do you actually have hundreds of megabytes you need to write out to disk?

I find I rarely need code to be ultra-efficient, but I almost always need it to be simple. The sort of difference in performance that you might see between this and a "clever" approach (if one is even available) isn't likely to be a complexity-changing effect (e.g. O(n) to O(log n)) - and that's the type of performance gain which really can be worth chasing.

EDIT: As noted in comments, .NET 4.0 has Stream.CopyTo so you don't need to code this up yourself.

Jon Skeet
Glorious, I guess I've fallen prey to ignoring the Stream class. Poor, poor Stream.
sixlettervariables
The file in question is between 5-10MB, so it is negligible in terms of speed. I was looking for something that was simple/concise (since simple/concise tends to mean efficient).
sixlettervariables
+2  A: 

The most concise solution I could think of is:


using(var resourceStream = assembly.GetManifestResourceStream(@"Namespace.Resources.File.ext"))
{
    byte[] buf = new byte[resourceStream.Length];
    File.WriteAllBytes(DEST_PATH, resourceStream.Read(buf, 0, buf.Length));
}

This can be done because the concrete type you'll get in resourceStream is a UnmanagedMemoryStream, which can be seeked and of which the length is known in advance.

emaster70