mirror of
https://github.com/markjprice/cs11dotnet7.git
synced 2026-04-04 22:07:39 +00:00
Initial commit
This commit is contained in:
parent
17ad36d6ad
commit
3d241b2154
44 changed files with 1619 additions and 0 deletions
78
vs4win/Chapter09/WorkingWithStreams/Program.Compress.cs
Normal file
78
vs4win/Chapter09/WorkingWithStreams/Program.Compress.cs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
using System.IO.Compression; // BrotliStream, GZipStream, CompressionMode
|
||||
using System.Xml; // XmlWriter, XmlReader
|
||||
|
||||
using static System.Environment; // CurrentDirectory
|
||||
using static System.IO.Path; // Combine
|
||||
|
||||
partial class Program
|
||||
{
|
||||
static void Compress(string algorithm = "gzip")
|
||||
{
|
||||
// define a file path using algorithm as file extension
|
||||
string filePath = Combine(
|
||||
CurrentDirectory, $"streams.{algorithm}");
|
||||
|
||||
FileStream file = File.Create(filePath);
|
||||
Stream compressor;
|
||||
if (algorithm == "gzip")
|
||||
{
|
||||
compressor = new GZipStream(file, CompressionMode.Compress);
|
||||
}
|
||||
else
|
||||
{
|
||||
compressor = new BrotliStream(file, CompressionMode.Compress);
|
||||
}
|
||||
|
||||
using (compressor)
|
||||
{
|
||||
using (XmlWriter xml = XmlWriter.Create(compressor))
|
||||
{
|
||||
xml.WriteStartDocument();
|
||||
xml.WriteStartElement("callsigns");
|
||||
foreach (string item in Viper.Callsigns)
|
||||
{
|
||||
xml.WriteElementString("callsign", item);
|
||||
}
|
||||
}
|
||||
} // also closes the underlying stream
|
||||
|
||||
// output all the contents of the compressed file
|
||||
WriteLine("{0} contains {1:N0} bytes.",
|
||||
filePath, new FileInfo(filePath).Length);
|
||||
|
||||
WriteLine($"The compressed contents:");
|
||||
WriteLine(File.ReadAllText(filePath));
|
||||
|
||||
// read a compressed file
|
||||
WriteLine("Reading the compressed XML file:");
|
||||
file = File.Open(filePath, FileMode.Open);
|
||||
Stream decompressor;
|
||||
if (algorithm == "gzip")
|
||||
{
|
||||
decompressor = new GZipStream(
|
||||
file, CompressionMode.Decompress);
|
||||
}
|
||||
else
|
||||
{
|
||||
decompressor = new BrotliStream(
|
||||
file, CompressionMode.Decompress);
|
||||
}
|
||||
|
||||
using (decompressor)
|
||||
{
|
||||
using (XmlReader reader = XmlReader.Create(decompressor))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
// check if we are on an element node named callsign
|
||||
if ((reader.NodeType == XmlNodeType.Element)
|
||||
&& (reader.Name == "callsign"))
|
||||
{
|
||||
reader.Read(); // move to the text inside element
|
||||
WriteLine($"{reader.Value}"); // read its value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
vs4win/Chapter09/WorkingWithStreams/Program.Helpers.cs
Normal file
12
vs4win/Chapter09/WorkingWithStreams/Program.Helpers.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
partial class Program
|
||||
{
|
||||
static void SectionTitle(string title)
|
||||
{
|
||||
ConsoleColor previousColor = ForegroundColor;
|
||||
ForegroundColor = ConsoleColor.Yellow;
|
||||
WriteLine("*");
|
||||
WriteLine($"* {title}");
|
||||
WriteLine("*");
|
||||
ForegroundColor = previousColor;
|
||||
}
|
||||
}
|
||||
94
vs4win/Chapter09/WorkingWithStreams/Program.cs
Normal file
94
vs4win/Chapter09/WorkingWithStreams/Program.cs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
using System.Xml;
|
||||
|
||||
using static System.Environment;
|
||||
using static System.IO.Path;
|
||||
|
||||
SectionTitle("Writing to text streams");
|
||||
|
||||
// define a file to write to
|
||||
string textFile = Combine(CurrentDirectory, "streams.txt");
|
||||
|
||||
// create a text file and return a helper writer
|
||||
StreamWriter text = File.CreateText(textFile);
|
||||
|
||||
// enumerate the strings, writing each one
|
||||
// to the stream on a separate line
|
||||
foreach (string item in Viper.Callsigns)
|
||||
{
|
||||
text.WriteLine(item);
|
||||
}
|
||||
text.Close(); // release resources
|
||||
|
||||
// output the contents of the file
|
||||
WriteLine("{0} contains {1:N0} bytes.",
|
||||
arg0: textFile,
|
||||
arg1: new FileInfo(textFile).Length);
|
||||
|
||||
WriteLine(File.ReadAllText(textFile));
|
||||
|
||||
SectionTitle("Writing to XML streams");
|
||||
|
||||
// define a file path to write to
|
||||
string xmlFile = Combine(CurrentDirectory, "streams.xml");
|
||||
|
||||
// declare variables for the filestream and XML writer
|
||||
FileStream? xmlFileStream = null;
|
||||
XmlWriter? xml = null;
|
||||
|
||||
try
|
||||
{
|
||||
// create a file stream
|
||||
xmlFileStream = File.Create(xmlFile);
|
||||
|
||||
// wrap the file stream in an XML writer helper
|
||||
// and automatically indent nested elements
|
||||
xml = XmlWriter.Create(xmlFileStream,
|
||||
new XmlWriterSettings { Indent = true });
|
||||
|
||||
// write the XML declaration
|
||||
xml.WriteStartDocument();
|
||||
|
||||
// write a root element
|
||||
xml.WriteStartElement("callsigns");
|
||||
|
||||
// enumerate the strings writing each one to the stream
|
||||
foreach (string item in Viper.Callsigns)
|
||||
{
|
||||
xml.WriteElementString("callsign", item);
|
||||
}
|
||||
|
||||
// write the close root element
|
||||
xml.WriteEndElement();
|
||||
|
||||
// close helper and stream
|
||||
xml.Close();
|
||||
xmlFileStream.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// if the path doesn't exist the exception will be caught
|
||||
WriteLine($"{ex.GetType()} says {ex.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (xml != null)
|
||||
{
|
||||
xml.Dispose();
|
||||
WriteLine("The XML writer's unmanaged resources have been disposed.");
|
||||
if (xmlFileStream != null)
|
||||
{
|
||||
xmlFileStream.Dispose();
|
||||
WriteLine("The file stream's unmanaged resources have been disposed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// output all the contents of the file
|
||||
WriteLine("{0} contains {1:N0} bytes.",
|
||||
arg0: xmlFile,
|
||||
arg1: new FileInfo(xmlFile).Length);
|
||||
WriteLine(File.ReadAllText(xmlFile));
|
||||
|
||||
SectionTitle("Compressing streams");
|
||||
Compress(algorithm: "gzip");
|
||||
Compress(algorithm: "brotli");
|
||||
9
vs4win/Chapter09/WorkingWithStreams/Viper.cs
Normal file
9
vs4win/Chapter09/WorkingWithStreams/Viper.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
static class Viper
|
||||
{
|
||||
// define an array of Viper pilot call signs
|
||||
public static string[] Callsigns = new[]
|
||||
{
|
||||
"Husker", "Starbuck", "Apollo", "Boomer",
|
||||
"Bulldog", "Athena", "Helo", "Racetrack"
|
||||
};
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="System.Console" Static="true" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue