/* Autor:           Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
   Firma:           HS Harz, FB AI
   Version:         1.1
   Ersterstellung:  23.03.2013
   Letzte Änderung: 23.03.2013
*/
import java.io.*;
import java.util.zip.*;

// Zip-Datei erstellen, aber eigenes Format

public class Read_Write_zip {


  private static void pause() {
    try {
      System.in.read();
    }
    catch (IOException e) {
    }
  }  // pause

  private static final int BLOCKSIZE = 8192;


  private static void test1()  {
    try
    {
     FileInputStream fis = new FileInputStream("original.txt");
     FileOutputStream fos = new FileOutputStream("deflated.txt");
     DeflaterOutputStream dos = new DeflaterOutputStream(fos);

     doCopy(fis, dos); // copy original.txt to deflated.txt and compress it
/*
     FileInputStream fis2 = new FileInputStream("deflated.txt");
     InflaterInputStream iis = new InflaterInputStream(fis2);
     FileOutputStream fos2 = new FileOutputStream("inflated.txt");
     doCopy(iis, fos2); // copy deflated.txt to inflated.txt and uncompress it
*/
   }
    catch ( IOException e ) {
      System.err.println( "Fehler beim Komprimieren "  );
    }
    }

  public static void doCopy(InputStream is, OutputStream os) throws IOException {
       int oneByte;
       while ((oneByte = is.read()) != -1) {
           os.write(oneByte);
       }
       os.close();
       is.close();
   }


  private static void test_zip(String sSourceFilename, String sDestFilename) {
     FileInputStream fin = null;
     FileOutputStream fout = null;
     DeflaterOutputStream dos = null;
    try
    {
        fin = new FileInputStream(sSourceFilename);
        fout = new FileOutputStream(sDestFilename);
        dos = new DeflaterOutputStream(fout);

      byte[] buffer = new byte[ BLOCKSIZE ];
      int laenge = fin.read(buffer, 0, BLOCKSIZE);
      while (laenge != -1 ) {
        dos.write( buffer, 0, laenge );
        laenge = fin.read(buffer, 0, BLOCKSIZE);
      }
    }
    catch ( IOException e ) {
      System.err.println( "Fehler beim Komprimieren " + sSourceFilename );
    }
    finally {
      if ( fin != null )
        try { 
           fin.close(); 
        } catch ( 
             IOException e ) { 
          e.printStackTrace(); 
       }
      if ( dos != null )
        try { 
          dos.close(); 
        } catch ( IOException e ) { 
             e.printStackTrace(); 
       }
    }
    System.out.println("Es wurde die Datei "+sDestFilename+" erzeugt");
    System.out.println("Bitte Taste druecken");
  }



  private static void test_unzip(String sSourceFilename, String sDestFilename) {
     FileInputStream fin = null;
     FileOutputStream fout = null;
     InflaterInputStream iis = null;
    try
    {
        fin = new FileInputStream(sSourceFilename);
        iis = new InflaterInputStream(fin);
        fout = new FileOutputStream(sDestFilename);
      
      byte[] buffer = new byte[ BLOCKSIZE ];
      int laenge = iis.read(buffer, 0, BLOCKSIZE);
      while (laenge != -1 ) {
        fout.write( buffer, 0, laenge );
        laenge = iis.read(buffer, 0, BLOCKSIZE);
      }

    }
    catch ( IOException e ) {
      System.err.println( "Fehler beim Komprimieren " + sSourceFilename );
    }
    finally {
      if ( fin != null )
        try { 
           fin.close(); 
        } catch ( 
             IOException e ) { 
          e.printStackTrace(); 
       }
      if ( iis != null )
        try { 
          iis.close(); 
        } catch ( IOException e ) { 
             e.printStackTrace(); 
       }
    }
    System.out.println("Es wurde die Datei "+sDestFilename+" entzippt");
    System.out.println("Bitte Taste druecken");
  }



  public static void main(String argv[]) {
    test1();
    test_zip("Write_zip.java", "Write_zip.zip");
    test_unzip("Write_zip.zip", "Write_zip2.java");
    pause();
  }  // main
}



