featurre fertig
parent
a1c9d3bdda
commit
a61b239846
110
Main.java
110
Main.java
|
@ -2,6 +2,8 @@ import java.util.HashMap;
|
|||
import java.util.*;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Stack;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class Main
|
||||
{
|
||||
|
@ -65,7 +67,14 @@ public class Main
|
|||
|
||||
printBaum(byteString_to_baum(baum_to_byteString(wurzel)));
|
||||
}
|
||||
|
||||
|
||||
public static void test3()
|
||||
{
|
||||
System.out.println(decodierungInlkBaum(codierungInklBaum("aaaabbc")));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void printBaum(Node baum)
|
||||
{
|
||||
if(baum == null)return;
|
||||
|
@ -130,6 +139,41 @@ public class Main
|
|||
System.out.println("Huffman-Codierung: " + code);
|
||||
return code;
|
||||
}
|
||||
|
||||
public static String codierungInklBaum(String text)
|
||||
{
|
||||
//zahl wan angibt wan text kommt und baum aufhört
|
||||
|
||||
//decodiere baum, dcodierung text
|
||||
|
||||
|
||||
Map<Character, Integer> zahlen = Zählen.countEachLetter(text);
|
||||
Node baum = Node.erstellen(zahlen);
|
||||
|
||||
String codierterBaum = baum_to_byteString(baum);
|
||||
String codierterText = codierung(text,baum);
|
||||
|
||||
int textErstIndex = codierterBaum.length() + 32;
|
||||
String codiertertextErstIndex = Int_to_ByteString(textErstIndex);
|
||||
|
||||
System.out.println(codiertertextErstIndex + codierterBaum +" " + codierterText);
|
||||
|
||||
return codiertertextErstIndex + codierterBaum + codierterText;
|
||||
}
|
||||
|
||||
public static String decodierungInlkBaum(String code)
|
||||
{
|
||||
String codierterTextErstIndex = code.substring(0,32);
|
||||
int textErstIndex = byteString_to_int(codierterTextErstIndex);
|
||||
|
||||
String codierterBaum = code.substring(32, textErstIndex);
|
||||
Node decodierterBaum = byteString_to_baum(codierterBaum);
|
||||
|
||||
String codierterText = code.substring(textErstIndex, code.length());
|
||||
String decodierterText = decodierungBaum(codierterText,decodierterBaum);
|
||||
|
||||
return decodierterText;
|
||||
}
|
||||
|
||||
public static String decodierungBaum(String code,Node baum)
|
||||
{
|
||||
|
@ -167,6 +211,8 @@ public class Main
|
|||
System.out.println(decoded);
|
||||
return decoded;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Byte ByteString_to_Byte (String byteString)
|
||||
{
|
||||
|
@ -314,5 +360,67 @@ public class Main
|
|||
return result;
|
||||
}
|
||||
|
||||
public static void compressFile (String source, String target )
|
||||
{
|
||||
Path sourcePath = Path.of(source);
|
||||
Path targetPath = Path.of(target);
|
||||
|
||||
if(Files.exists(sourcePath) == false)
|
||||
{
|
||||
System.out.println("Datei nicht gefunden");
|
||||
return;
|
||||
}
|
||||
|
||||
String text;
|
||||
|
||||
try
|
||||
{
|
||||
text = Files.readString(sourcePath);
|
||||
}
|
||||
catch(Exception error)
|
||||
{
|
||||
System.out.println(error);
|
||||
return;
|
||||
}
|
||||
|
||||
String compressedText = codierungInklBaum(text);
|
||||
|
||||
try
|
||||
{
|
||||
Files.writeString(targetPath, compressedText);
|
||||
}
|
||||
catch(Exception error)
|
||||
{
|
||||
System.out.println(error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void readFile(String file)
|
||||
{
|
||||
Path path = Path.of(file);
|
||||
|
||||
if(Files.exists(path) == false)
|
||||
{
|
||||
System.out.println("Datei nicht gefunden");
|
||||
return;
|
||||
}
|
||||
|
||||
String compressedText;
|
||||
|
||||
try
|
||||
{
|
||||
compressedText = Files.readString(path);
|
||||
}
|
||||
catch(Exception error)
|
||||
{
|
||||
System.out.println(error);
|
||||
return;
|
||||
}
|
||||
|
||||
String text = decodierungInlkBaum(compressedText);
|
||||
|
||||
System.out.println(file+": " + text);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
000000000000000000000000101100001111111111111111000000000110000111111111111111110000000001100010111111111111111100000000000010101111111111111111000000000110001100000000000011010000101011101111110
|
Loading…
Reference in New Issue