// ZIPDateien.cpp: Hauptprojektdatei.

#include "stdafx.h"

using namespace System;
using namespace System::IO;
using namespace System::IO::Compression;

int main(array<System::String ^> ^args)
{
   /* 
    * Text erzeugen 
    */
   StringWriter^ strw = gcnew StringWriter();

   for(int n = 0; n < 1000; n++)
   {
      strw->WriteLine(" Seltsam, im Nebel zu wandern!");
      strw->WriteLine(" Einsam ist jeder Busch und Stein,");
      strw->WriteLine(" Kein Baum sieht den andern,");
      strw->WriteLine(" Jeder ist allein.");
      strw->WriteLine(" ...");
      strw->WriteLine("                        Hermann Hesse");
   }

   String^ text = strw->ToString();

   /*
    * in nicht komprimierte Datei schreiben
    */
   {
      FileStream^ fs = gcnew FileStream("ImNebel.txt", FileMode::Create);

      StreamWriter^ sw = gcnew StreamWriter(fs);
      sw->WriteLine(text);
      sw->Close();
   }


   /*
    * komprimiert in Datei schreiben
    */
   {
      FileStream^ fs = gcnew FileStream("ImNebel.gz", FileMode::Create);
      GZipStream^ zs = gcnew GZipStream(fs, CompressionMode::Compress);
      StreamWriter^ sw = gcnew StreamWriter(zs);

      sw->WriteLine(text);
      sw->Close();
   }

   /*
    * aus komprimierter Datei lesen
    */
   {
      FileStream^ fs = gcnew FileStream("ImNebel.gz", FileMode::Open);
      GZipStream^ zs = gcnew GZipStream(fs, CompressionMode::Decompress);
      StreamReader^ sr = gcnew StreamReader(zs);

      Console::WriteLine("\n Anfang von ImNebel.gz \n");
      String^ zeile;
      for (int n = 0; n < 10; n++)
      { 
         if ((zeile = sr->ReadLine()) != nullptr)
            Console::WriteLine(zeile);
      }
      sr->Close();
   }

   /*
    * Datei komprimieren
    */
   {
      FileStream^ fs_text = gcnew FileStream("ImNebel.txt", FileMode::Open);
      FileStream^ fs_zip = gcnew FileStream("ImNebelKomprimiert.gz", FileMode::Create);
      GZipStream^ zs = gcnew GZipStream(fs_zip, CompressionMode::Compress);

      fs_text->CopyTo(zs);
      zs->Close();
      fs_text->Close();

      // aus komprimierter Datei lesen
      fs_zip = gcnew FileStream("ImNebelKomprimiert.gz", FileMode::Open);
      zs = gcnew GZipStream(fs_zip, CompressionMode::Decompress);
      StreamReader^ sr = gcnew StreamReader(zs);

      Console::WriteLine("\n\n\n Anfang von ImNebelKomprimiert.gz \n");
      String^ zeile;
      for (int n = 0; n < 10; n++)
      { 
         if ((zeile = sr->ReadLine()) != nullptr)
            Console::WriteLine(zeile);
      }
      sr->Close();
   }

   Console::ReadLine();
   return 0;
}
