Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 73 lines (58 sloc) 2.455 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.IO.Compression;
using System.Runtime.InteropServices;

namespace MediaInfoProvider {

    public class LibraryLoader {

[DllImport("kernel32")]
        static extern IntPtr LoadLibrary(string lpFileName);

        public static void Extract(string name, string target) {
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name)) {
                GZipStream unzippedStream = new GZipStream(stream, CompressionMode.Decompress, true);

                try {
                    File.WriteAllBytes(target, ReadStream(unzippedStream, 0));
                } catch {
                    // nothing we can do, just try to load whats there
                }
            }
        }

        // from http://www.yoda.arachsys.com/csharp/readbinary.html
        private static byte[] ReadStream(Stream stream, int initialLength) {

            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1) {
                initialLength = 32768;
            }

            byte[] buffer = new byte[initialLength];
            int read = 0;

            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0) {
                read += chunk;

                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length) {
                    int nextByte = stream.ReadByte();

                    // End of stream? If so, we're done
                    if (nextByte == -1) {
                        return buffer;
                    }

                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }

    }


}
Something went wrong with that request. Please try again.