Esempio n. 1
0
 internal virtual void FileStreamCopy(Stream inputStream, string fileFullPath)
 {
     using (var outputStream = NuGetExtractionFileIO.CreateFile(fileFullPath))
     {
         CopyTo(inputStream, outputStream);
     }
 }
Esempio n. 2
0
        public static string CopyToFile(this Stream inputStream, string fileFullPath)
        {
            if (Path.GetFileName(fileFullPath).Length == 0)
            {
                Directory.CreateDirectory(fileFullPath);
                return(fileFullPath);
            }

            var directory = Path.GetDirectoryName(fileFullPath);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            if (File.Exists(fileFullPath))
            {
                // Log and skip adding file
                return(fileFullPath);
            }

            using (var outputStream = NuGetExtractionFileIO.CreateFile(fileFullPath))
            {
                inputStream.CopyTo(outputStream);
            }

            return(fileFullPath);
        }
Esempio n. 3
0
        public static string CopyToFile(this Stream inputStream, string fileFullPath)
        {
            if (Path.GetFileName(fileFullPath).Length == 0)
            {
                Directory.CreateDirectory(fileFullPath);
                return(fileFullPath);
            }

            var directory = Path.GetDirectoryName(fileFullPath);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            if (File.Exists(fileFullPath))
            {
                // Log and skip adding file
                return(fileFullPath);
            }

            // For files of a certain size, we can do some Cleverness and mmap
            // them instead of writing directly to disk. This can improve
            // performance by a lot on some operating systems and hardware,
            // particularly Windows
            long?size = null;

            try
            {
                size = inputStream.Length;
            }
            catch (NotSupportedException)
            {
                // If we can't get Length, just move on.
            }
            using (var outputStream = NuGetExtractionFileIO.CreateFile(fileFullPath))
            {
                if (size > 0 && size <= MAX_MMAP_SIZE)
                {
                    // NOTE: Linux can't create a mmf from outputStream, so we
                    // need to close the file (which now has the desired
                    // perms), and then re-open it as a memory-mapped file.
                    outputStream.Dispose();
                    using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileFullPath, FileMode.Open, mapName: null, (long)size))
                        using (MemoryMappedViewStream mmstream = mmf.CreateViewStream())
                        {
                            inputStream.CopyTo(mmstream);
                        }
                }
                else
                {
                    inputStream.CopyTo(outputStream);
                }
            }
            return(fileFullPath);
        }
Esempio n. 4
0
 internal virtual void MmapCopy(Stream inputStream, string fileFullPath, long size)
 {
     using (var outputStream = NuGetExtractionFileIO.CreateFile(fileFullPath))
     {
         // NOTE: Linux can't create a mmf from outputStream, so we
         // need to close the file (which now has the desired
         // perms), and then re-open it as a memory-mapped file.
         outputStream.Dispose();
         using (MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fileFullPath, FileMode.Open, mapName: null, size))
             using (MemoryMappedViewStream mmstream = mmf.CreateViewStream())
             {
                 CopyTo(inputStream, mmstream);
             }
     }
 }
        public string ExtractPackageFile(string source, string target, Stream stream)
        {
            if ((_xmlDocFileSaveMode == XmlDocFileSaveMode.Skip) && _intellisenseXmlFiles.Contains(source))
            {
                return(null);
            }

            var extractDirectory = Path.GetDirectoryName(target);

            Directory.CreateDirectory(extractDirectory);

            if ((_xmlDocFileSaveMode == XmlDocFileSaveMode.Compress) && _intellisenseXmlFiles.Contains(source))
            {
                // If the package contains a file named {BinaryName}.xml.zip already exists, the result would be
                // ambigious.
                var targetZip = Path.ChangeExtension(target, ".xml.zip");
                using (var outputStream = NuGetExtractionFileIO.CreateFile(targetZip))
                    using (var zipArchive = new ZipArchive(outputStream, ZipArchiveMode.Create))
                    {
                        var entry = zipArchive.CreateEntry(Path.GetFileName(source));
                        using (var entryStream = entry.Open())
                        {
                            stream.CopyTo(entryStream);
                        }
                    }

                return(targetZip);
            }

            if (Path.IsPathRooted(source))
            {
                // Copying files stream-to-stream is less efficient. Attempt to copy using File.Copy if we the source
                // is a file on disk.
                File.Copy(source, target, overwrite: true);
            }
            else
            {
                stream.CopyToFile(target);
            }

            return(target);
        }