Exemplo n.º 1
0
        public static MemoryStream AsMemoryStreamFromFile(string fileName)
        {
            SentinelHelper.IsTrue(string.IsNullOrEmpty(fileName));

            MemoryStream ms;
            FileStream   fs     = null;
            MemoryStream mstemp = null;

            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                try
                {
                    mstemp = new MemoryStream(fs.AsByteArray());
                    ms     = mstemp;
                    mstemp = null;
                }
                finally
                {
                    if (mstemp != null)
                    {
                        mstemp.Dispose();
                    }
                }
            }
            finally
            {
                if (fs != null)
                {
                    fs.Dispose();
                }
            }

            return(ms);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a stream with with specified element compressed
        /// </summary>
        /// <param name="elements">Zip elements.</param>
        /// <returns>
        /// A <see cref="Stream"/> than contains zipped file.
        /// </returns>
        public static Stream ToZipStream(this IEnumerable <byte[]> elements)
        {
            IList <byte[]> elementList = elements as IList <byte[]> ?? elements.ToList();

            SentinelHelper.ArgumentNull(elementList, nameof(elements));

            iTinIO.File.CleanOrCreateTemporaryDirectory();

            Uri    zipNameTempUri              = iTinIO.File.GetUniqueTempRandomFile();
            string zipNameLocalPath            = Path.GetFileName(zipNameTempUri.LocalPath);
            string tempDirectory               = iTinIO.File.TempDirectoryFullName;
            string zipFullPath                 = Path.Combine(tempDirectory, zipNameLocalPath);
            string zipFilenameWithExtension    = Path.GetFileName(zipFullPath);
            string zipFilenameWithoutExtension = Path.GetFileNameWithoutExtension(zipFullPath);

            bool existPath = File.Exists(zipFullPath);

            if (existPath)
            {
                File.Delete(zipFullPath);
            }

            string     outputStreamName     = $"_{zipFilenameWithExtension}";
            string     outputStreamFullPath = Path.Combine(tempDirectory, outputStreamName);
            FileStream fs = new FileStream(outputStreamFullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);

            using (var zip = new ZipFile(zipFullPath))
            {
                int           currentFile      = 0;
                bool          multipleElements = elementList.Count > 1;
                StringBuilder filenameBuilder  = new StringBuilder();
                foreach (var element in elementList)
                {
                    filenameBuilder.Clear();
                    filenameBuilder.Append(zipFilenameWithoutExtension);

                    if (multipleElements)
                    {
                        filenameBuilder.Append(currentFile);
                    }

                    filenameBuilder.Append(".");
                    filenameBuilder.Append("pdf");
                    zip.AddEntry(filenameBuilder.ToString(), element);
                    currentFile++;
                }

                zip.Save(fs);
            }

            return(fs.AsByteArray().ToMemoryStream());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns the specified file as a byte array.
        /// </summary>
        /// <param name="fileName">File to convert.</param>
        /// <returns>
        /// Array of byte than represent the file.
        /// </returns>
        public static byte[] AsByteArrayFromFile(string fileName)
        {
            SentinelHelper.IsTrue(string.IsNullOrEmpty(fileName));

            byte[] buffer;

            using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                buffer = stream.AsByteArray();
            }

            return(buffer);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns the specified file as a byte array.
        /// </summary>
        /// <param name="fileName">File to convert.</param>
        /// <returns>
        /// Array of byte than represent the file.
        /// </returns>
        public static byte[] AsByteArrayFromFile(string fileName)
        {
            Logger.Instance.Debug("External Call");
            Logger.Instance.Info("  Returns the specified file as a byte array");
            Logger.Instance.Info("  > Library: iTin.Core");
            Logger.Instance.Info("  > Class: StreamHelper");
            Logger.Instance.Info("  > Method: AsByteArrayFromFile(string)");
            Logger.Instance.Info("  > Output: System.Byte[]");

            SentinelHelper.IsTrue(string.IsNullOrEmpty(fileName));

            byte[] buffer;

            using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                buffer = stream.AsByteArray();
            }

            return(buffer);
        }
Exemplo n.º 5
0
        public static MemoryStream AsMemoryStreamFromFile(string fileName)
        {
            Logger.Instance.Debug("External Call");
            Logger.Instance.Info("  Convert input file to memory stream");
            Logger.Instance.Info("  > Library: iTin.Core");
            Logger.Instance.Info("  > Class: StreamHelper");
            Logger.Instance.Info("  > Method: AsMemoryStreamFromFile(string)");
            Logger.Instance.Info("  > Output: System.IO.MemoryStream");

            SentinelHelper.IsTrue(string.IsNullOrEmpty(fileName));

            MemoryStream ms;
            FileStream   fs     = null;
            MemoryStream mstemp = null;

            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                try
                {
                    mstemp = new MemoryStream(fs.AsByteArray());
                    ms     = mstemp;
                    mstemp = null;
                }
                finally
                {
                    mstemp?.Dispose();
                }
            }
            finally
            {
                fs?.Dispose();
            }

            return(ms);
        }