نمایش نتایج 1 تا 6 از 6

نام تاپیک: فشرده سازی اطلاعات با استفاده از کد هافمن

Threaded View

پست قبلی پست قبلی   پست بعدی پست بعدی
  1. #5

    نقل قول: فشرده سازی اطلاعات با استفاده از کد هافمن

    ممنون از راهنماییتون..من با توجه دانشی دارم فقط در این حد تونستم تعغیرش بدم که بازم جواب نداد


    بنظرتون چکار کنم؟






    package darseOne;


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.math.BigInteger;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.HashMap;
    import java.util.PriorityQueue;
    import java.util.Scanner;
    import java.util.TreeMap;








    public class yek {

    static String bytecods = "";
    static String encoded = "";
    static String decoded = "";
    static int plus = 0;
    static int[] ASKY = new int[65536];
    static PriorityQueue<Node> nodes = new PriorityQueue<>((o1, o2) -> (o1.value < o2.value) ? -1 : 1);
    static TreeMap<Character, String> maps = new TreeMap<>();
    static Scanner ss;
    static FileReader reader;
    static FileInputStream inputStream;
    static byte[] bi1;




    public static void main(String[] args) {

    try {


    // inputStream = new FileInputStream("moji/file/test");
    // ss = new Scanner(inputStream,"Cp1252");

    Path path = Paths.get("moji/file/test");
    bi1 = Files.readAllBytes(path);


    } catch (Exception e1) {
    e1.printStackTrace();
    }

    StringBuffer buff = new StringBuffer();
    // while (ss.hasNextLine()) {
    // buff.append(ss.nextLine()+"\n");
    //
    // }



    bytecods = new String(bi1);
    System.out.println("1 copid file");
    for(int i = 0 ; i < ASKY.length ; i++){
    ASKY[i]=0;
    }
    System.out.println("2 creat aski code");
    for (int i = 0 ; i < bytecods.length(); i++){
    ASKY[bytecods.charAt(i)]++;
    }
    System.out.println("3 Counts alphabet");
    for (int i = 0 ; i < ASKY.length ; i++){
    if(ASKY[i]!=0){
    nodes.add(new Node(ASKY[i], (char)i));
    }
    }
    System.out.println("4 creat tree barg");
    while(nodes.size()>1){
    //add kardan
    nodes.add(new Node(nodes.poll(),nodes.poll()));
    }
    System.out.println("5 creat tree root");
    //creat binari
    creatBinari(nodes.peek(),"");
    System.out.println("6 change tree to binari");
    String encod = Encoding();
    while (encod.length()%8!=0) {
    encod = "0"+encod;
    plus++;
    }
    System.out.println("7 encoding file");
    try {
    FileOutputStream fos = new FileOutputStream("moji/Compressed/encode.txt");
    fos.write(encod.getBytes());
    fos.close();
    System.out.println("* convert 0 & 1 to file");


    } catch (IOException e) {
    e.printStackTrace();
    }
    String[] hash = new String[encod.length()/8];
    int j = encod.length()-8;
    int jj = encod.length();
    for(int i=0;i<hash.length;i++){
    hash[hash.length-i-1] =encod.substring(j, jj);
    j=j-8;
    jj=jj-8;
    }

    byte[] byt = new byte[hash.length];
    for(int i=0;i<byt.length;i++){
    int b=0;
    int komak =hash[i].length();
    for(int ii=0;ii<komak;ii++){
    b = b + (Integer.parseInt(hash[i].substring(komak-ii-1, komak-ii))*((int)Math.pow(2,ii)));
    }
    byt[i] = (byte) b;
    }
    try{
    FileOutputStream fos = new FileOutputStream("moji/Compressed/compress.moji");
    fos.write(byt);
    fos.close();
    }catch(Exception e){

    }
    System.out.println("8 convert to byte");
    System.out.println("start decomperes");
    try{
    FileInputStream in = new FileInputStream("moji/Compressed/compress.moji");
    byte chunk[] = new byte[(encod.length()/8)+1];
    in.read(chunk, 0, (encod.length()/8)+1);

    StringBuffer buf = new StringBuffer();
    for (byte b : chunk){
    String s = String.format("%8s", Integer.toBinaryString(b & 0xFF));
    s=s.replace(" ", "0");
    buf.append(s);
    }
    String baz = buf.toString();
    baz = baz.substring(plus);
    encod = baz;

    in.close();
    }catch(Exception e){
    System.out.println("no");
    }
    String fin= Decoded();
    try {
    Path path = Paths.get("moji/baz/baz");
    Files.write(path, fin.getBytes());
    System.out.println("done");


    } catch (IOException e) {
    e.printStackTrace();
    }

    if(fin.equals(bytecods)){
    System.out.println("***yes***");
    }else {
    System.out.println("---no---");
    }

    }

    static String Decoded(){
    decoded="";
    StringBuffer b = new StringBuffer();
    for(int i=0;i<encoded.length();){
    Node n = nodes.peek();
    while(n.left != null && n.right != null){
    if(encoded.charAt(i)== '0'){
    n=n.left ;

    }else if(encoded.charAt(i)== '1'){
    n=n.right;
    }
    i++;
    }
    b.append(n.character);


    }
    return b.toString();
    }
    static String Encoding(){
    encoded = "";
    String k = bytecods;
    StringBuffer buffer = new StringBuffer();
    for(int i=0 ; i<bytecods.length();i++){
    buffer.append(maps.get(bytecods.charAt(i)));
    }
    encoded = buffer.toString();
    return encoded;
    }
    static void creatBinari(Node node,String s){
    if(node != null){
    if(node.left != null){
    creatBinari(node.left,s+"0");
    }
    if(node.right != null){
    creatBinari(node.right,s+"1");
    }

    if(node.right == null && node.left == null){
    maps.put(node.character,s);
    }
    }

    }


    }




    class Node{
    Character character;
    Double value;
    Node left,right;
    public Node(double value,Character character) {
    this.value = value;
    this.character = character;
    left = null;
    right = null;

    }
    public Node(Node left, Node right) {
    this.value = left.value + right.value;
    this.character = null;
    if (left.value < right.value) {
    this.right = right;
    this.left = left;
    } else {
    this.right = left;
    this.left = right;
    }

    }
    }


    آخرین ویرایش به وسیله hemmaty76 : شنبه 29 اردیبهشت 1397 در 22:45 عصر

تاپیک های مشابه

  1. فشرده سازی تصویر با استفاده از SVD
    نوشته شده توسط aref_error در بخش برنامه نویسی Matlab
    پاسخ: 1
    آخرین پست: چهارشنبه 15 مرداد 1393, 21:48 عصر
  2. اشکال در فشرده سازی صفحات با استفاده از gzip
    نوشته شده توسط Kia_MLS در بخش ASP.NET Web Forms
    پاسخ: 2
    آخرین پست: چهارشنبه 20 بهمن 1389, 09:37 صبح
  3. نمایش اطلاعات در datagrid با استفاده از کد
    نوشته شده توسط mah6951 در بخش کامپوننت ها و ابزارهای کاربردی در VB6
    پاسخ: 3
    آخرین پست: پنج شنبه 03 تیر 1389, 02:02 صبح

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •