Exemplo n.º 1
0
        private static void CopyFile(Path src, Path dst)
        {
            var fhSrc = IoLongPath.CreateFile(
                src.LongPath,
                IoLongPath.EFileAccess.GenericRead,
                IoLongPath.EFileShare.Read,
                IntPtr.Zero,
                IoLongPath.ECreationDisposition.OpenExisting,
                0,
                IntPtr.Zero);

            // Check for errors
            var lastWin32Error = Marshal.GetLastWin32Error();

            if (fhSrc.IsInvalid)
            {
                throw new System.ComponentModel.Win32Exception(lastWin32Error);
            }

            using (var fsSrc = new FileStream(fhSrc, FileAccess.Read))
            {
                var fhDst = IoLongPath.CreateFile(
                    dst.LongPath,
                    IoLongPath.EFileAccess.GenericWrite,
                    IoLongPath.EFileShare.None,
                    IntPtr.Zero,
                    IoLongPath.ECreationDisposition.CreateAlways,
                    0,
                    IntPtr.Zero);

                // Check for errors
                lastWin32Error = Marshal.GetLastWin32Error();
                if (fhDst.IsInvalid)
                {
                    throw new System.ComponentModel.Win32Exception(lastWin32Error);
                }

                using (var fsDst = new FileStream(fhDst, FileAccess.Write))
                {
                    var b = new byte[1024];
                    int n;
                    while ((n = fsSrc.Read(b, 0, b.Length)) > 0)
                    {
                        fsDst.Write(b, 0, n);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static void ZipFile(Path src, Path dst)
        {
            var fhSrc = IoLongPath.CreateFile(
                src.LongPath,
                IoLongPath.EFileAccess.GenericRead,
                IoLongPath.EFileShare.Read,
                IntPtr.Zero,
                IoLongPath.ECreationDisposition.OpenExisting,
                0,
                IntPtr.Zero);

            // Check for errors
            var lastWin32Error = Marshal.GetLastWin32Error();

            if (fhSrc.IsInvalid)
            {
                throw new System.ComponentModel.Win32Exception(lastWin32Error);
            }

            using (var fsSrc = new FileStream(fhSrc, FileAccess.Read))
            {
                // Create destination zip file
                var fhDst = IoLongPath.CreateFile(
                    dst.LongPath,
                    IoLongPath.EFileAccess.GenericWrite,
                    IoLongPath.EFileShare.None,
                    IntPtr.Zero,
                    IoLongPath.ECreationDisposition.CreateAlways,
                    0,
                    IntPtr.Zero);

                // Check for errors
                lastWin32Error = Marshal.GetLastWin32Error();
                if (fhDst.IsInvalid)
                {
                    throw new System.ComponentModel.Win32Exception(lastWin32Error);
                }

                using (var fsDst = new FileStream(fhDst, FileAccess.Write))
                {
                    using (var archive = new ZipArchive(fsDst, ZipArchiveMode.Create))
                    {
                        var readmeEntry = archive.CreateEntry(src.GetFilename);
                        //using (var writer = new StreamWriter(readmeEntry.Open()))
                        using (var writer = new BinaryWriter(readmeEntry.Open()))
                        {
                            byte[] b = new byte[1024];
                            int    n;
                            while ((n = fsSrc.Read(b, 0, b.Length)) > 0)
                            {
                                writer.Write(b, 0, n);
                            }
                        }
                    }
                    //byte[] b = new byte[1024];
                    //int n;
                    //while ((n = fsSrc.Read(b, 0, b.Length)) > 0)
                    //{
                    //	fsDst.Write(b, 0, n);
                    //}
                }
            }
        }