C# Stream

using System;
using System.IO;
using System.IO.Compression;

public class Program
{
    public static void Compress(DirectoryInfo directorySelected)
    {
      using (FileStream compressedFileStream = File.Create(file.FullName + ".cmp"))
      {
        using (DeflateStream compressionStream = new DeflateStream(compressedFileStream, CompressionMode.Compress))
        {
          originalFileStream.CopyTo(compressionStream);
        }
      }
    }

    public static void Decompress(FileInfo fileToDecompress)
    {
      using (FileStream decompressedFileStream = File.Create(newFileName))
      {
        using (DeflateStream decompressionStream = new DeflateStream(originalFileStream, CompressionMode.Decompress))
        {
          decompressionStream.CopyTo(decompressedFileStream);
          Console.WriteLine("Decompressed: {0}", fileToDecompress.Name);
        }
      }
    }
}

3.63
8
Krish 100200 points

                                        private static void SplitFile(string inputFile, int chunkSize, string path)
    {
        byte[] buffer = new byte[chunkSize];
        List<byte> extraBuffer = new List<byte>();

        using (Stream input = File.OpenRead(inputFile))
        {
            int index = 0;
            while (input.Position < input.Length)
            {
                using (Stream output = File.Create(path + "\\" + index + ".csv"))
                {
                    int chunkBytesRead = 0;
                    while (chunkBytesRead < chunkSize)
                    {
                        int bytesRead = input.Read(buffer,
                                                   chunkBytesRead,
                                                   chunkSize - chunkBytesRead);

                        if (bytesRead == 0)
                        {
                            break;
                        }

                        chunkBytesRead += bytesRead;
                    }

                    byte extraByte = buffer[chunkSize - 1];
                    while (extraByte != '\n')
                    {
                        int flag = input.ReadByte();
                        if (flag == -1)
                            break;
                        extraByte = (byte)flag;
                        extraBuffer.Add(extraByte);
                    }

                    output.Write(buffer, 0, chunkBytesRead);
                    if (extraBuffer.Count > 0)
                        output.Write(extraBuffer.ToArray(), 0, extraBuffer.Count);

                    extraBuffer.Clear();
                }
                index++;
            }
        }
    }

3.63 (8 Votes)
0
0
7
A-312 69370 points

                                    //this will get a string with all the text from the file
var fileText = File.ReadAllText(@"path\to\my\file.txt");

//this will get all of the lines of the file as an string[]
var fileLines = File.ReadAllLines(@"path\to\my\file.txt");

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
open file using stream c# c# stream collection c#video file reader stream stream reader in c# stream in c# tutorial get file as stream c# c# read stream to file c# stream read stream c# example c# Stream use c# stream object c# reading a file using stream how to open file stream c# java stream c# file open as stream c# c# stream tutorial open file in stream c# c# Stream.Read string stream c# c# stream class c# display file from stream .net stream read file what is stream in c# with example c# read a file from stream how to read a stream in c# stream read in c# using (FileStream stream = File.Open c# c# - read from file stream how can use c# stream stream object c# stream read example c# java stream in c# create stream from stream c# what is a stream in c# open file stream c# c# how to work with stream C# file to stream read file as stream c# c# system.io.stream read file file stream in c# c# read a stream using stream in c# c# read write stream how to read from stream in c# using stream c# read file from stream stream string c# read a file stream c# stream open and read c# c# return file stream c# stream reader from file info what is stream in C# definition of stream in c# Stream c# how to read a file stream in c# c# read file into stream c# console application read stream read stream file read from stream c# file to read stream c# load file to stream .net file stream Stream fileaccess c# open file with stream c# open file as stream c# stream.read c# stream reader vs reading stream directly c# filestream read c# c# write file stream c# stream file to stream c# c# stream from file how to read data in file with stream reader c# c# read from stream c# file stream read stream read c# c# file as stream deflate stream c# c# stream from filer C# stream of file c# filestream from stream c# file from stream read stream from a file c# c# read stream stream from file c# file streams in c# file stream c# read file into stream c# c# get stream from file what is c# file stream c# filestream read file to end c# read file to stream c# read file from stream read file stream c# get file as stream c# c# read file in stream c# reading file data from stream .net read file file output stream fileinputstream read into string c#.nett Read file as a stream c# c# using stream how to open a read stream with using in cE get binary from filestream stream in c# streamreader c# read all file return file stream c# how to read data from a stream reader c# streamwriter C# stream reader c# filestream read data from filestream c# read file as stream Stream GetFileContent( how to read file as stream in c# stream read from from in .net core read stream c# how to creagea stream readers c# StringReader fileReader= File.OpenText(finalPath); stream reader c# how to open a file usung a stream reader c# DEFLATE open file and read it c# using streamreader read file as stream C# read file without stream reader c# read file using streamreader text stream reader read file from stream c# c# reat stream file filestream read file c# filestream read function c filestream read function using file stream read and array buffer C# get a stream from file c# stream load file how to read bits from filestream in C# .read() function in filestream using stream to read file c# filestream read all bytes open file from stream using octet-stream c# c# file stream get file from filestream c# c# read as stream to file filestream c# read file file stream buffer c# new filestream c# from byte stream FileStream read from file c# filereader read file to stream c# get file from filestream how to get the file stream of a file c# stream readfile c# c# .net load file to stream c# filestream read c# load file into stream C# read file stream and display on page c# read file using buffer deflate stream read file content buffer C# c# read file streamreader vb.net stream read file filestream.read c# get file stream c# c# stream to text file c# wpf read log file in thread read file rust read filestream c# read from file to stream c# how to load file stream in application with c# read fstream c# c# file read stream c# read file stream
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source