الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

LZW Compression

بدأه programing*gril في 17 مارس 2011 · 6 رد · 1,577 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

ما يلي كود لطريقة الـ lzw compression

لقد قمت بعمل كود للـ compression

ولكن هناك run time error

ارجو ممن لديه القدرة افادتي

package homework;
import java.io.*;

public class lzw
{
	public static void main ( String[] args )
	{
		FileInputStream  input  = null;
		FileOutputStream output = null;

		if( args[0] == "" )
		{
			System.out.println( "Usage: java lzw <filename>" );
			System.exit( 1 );
		}

		try
		{
			input = new FileInputStream( args[0] );
		}
		catch ( FileNotFoundException fnfe )
		{
			System.out.println( "Unable to open input file: " + args[0] );
			System.exit( 1 );
		}

		try
		{
			output = new FileOutputStream( "compressed.lzw" );
		}
		catch ( FileNotFoundException fnfe )
		{
			System.out.println( "Unable to open output file compressed.lzw " );
			System.exit( 1 );
		}

		LZWCompression lzw = new LZWCompression( input, output );

		lzw.compress();		/* compress the file */

		try
		{
			input.close();
			output.close();
		}
		catch ( IOException ioe )
		{
			System.out.println( "IOException in main()." );
			System.exit(1);
		}

		System.out.println( "Done! Compressed file: compressed.lzw");
	}
}

package homework;
import java.io.*;

public class LZWCompression {

	private static final int BITS 			= 	12;
	private static final int HASHING_SHIFT 	= 	4;
	private static final int MAX_VALUE 		= 	(1 << BITS ) - 1;
	private static final int MAX_CODE 		= 	MAX_VALUE - 1;
	private static final int TABLE_SIZE 	= 	5021;
	private static final int EOF			= 	-1;

	private BufferedInputStream  input  = null;
	private BufferedOutputStream output = null;

	private int output_bit_count  = 0;
	private int output_bit_buffer = 0;

	private short[] code_value  		= new short[ TABLE_SIZE ];
	private short[] prefix_code 		= new short[ TABLE_SIZE ];
	private short[] append_character 	= new short[ TABLE_SIZE ];

	LZWCompression ( FileInputStream input, FileOutputStream output )
	{
		this.input  = new BufferedInputStream(  input  );
		this.output = new BufferedOutputStream( output );
	}

	public void compress()
	{
		short next_code   = 0;
		short character   = 0;
		short string_code = 0;
		short index 	  = 0;

		next_code = 256;              			  /* Next code is the next available string code*/

		for ( short i = 0; i < TABLE_SIZE; i++ )  /* Clear out the string table before starting */
			code_value[ i ] = -1;

		try
		{
			string_code = ( short ) input.read(); /* Get the first character. Assuming it to be 0 - 255
			 									  ** Hence only valid for ASCII text files */

			/*
			** This is the main loop where it all happens.  This loop runs util all of
			** the input has been exhausted.  Note that it stops adding codes to the
			** table after all of the possible codes have been defined.
			*/
			while ( ( character = ( short ) input.read() ) != EOF )
			{
				index = find_match ( string_code, character );	/* See if the string is in */

				if ( code_value[ index ] != -1 )
			    {            									/* the table.  If it is,   */
					string_code = code_value[ index ];          /* get the code value.  If */
			    }
			    else                                    		/* the string is not in the*/
			    {                                       		/* table, try to add it.   */
			    	if ( next_code <= MAX_CODE )
			    	{
					    code_value		[ index ] = next_code++;
					    prefix_code		[ index ] = string_code;
					    append_character[ index ] = character;
			    	}

			        output_code( string_code );  				/* When a string is found  */
			    	string_code = character;           			/* that is not in the table */
			    }												/* I output the last string */
			}                                     				/* after adding the new one */

			/*
			** End of the main loop.
			*/

			output_code( string_code ); 						/* Output the last code               */
			output_code( ( short ) MAX_VALUE );   				/* Output the end of buffer code      */
			output_code( ( short ) 0 );           				/* This code flushes the output buffer*/

			/*
			** Close the files
			*/
			output.close();
			input.close();
		}
		catch ( IOException ioe )
		{
			System.out.println( "IOException in compress()" );
			System.exit( 1 );
		}
	}

	/*
	** This is the hashing routine.  It tries to find a match for the prefix+char
	** string in the string table.  If it finds it, the index is returned.  If
	** the string is not found, the first available index in the string table is
	** returned instead.
	*/
	private short find_match ( short hash_prefix, short hash_character )
	{
		int index  = 0;
		int offset = 0;

		index = ( hash_character << HASHING_SHIFT ) ^ hash_prefix;

		if ( index == 0 )
			offset = 1;
		else
			offset = TABLE_SIZE - index;

		while ( true )
		{
			if ( code_value[ index ] == -1 )
				return ( short ) index;
			if ( prefix_code[ index ] == hash_prefix && append_character[ index ] == hash_character )
				return ( short ) index;

			index -= offset;

			if ( index < 0 )
				index += TABLE_SIZE;
		}
	}

	private void output_code( short code )
	{
		output_bit_buffer |= code << ( 32 - BITS - output_bit_count );
		output_bit_count += BITS;

		while ( output_bit_count >= 8 )
		{
			try
			{
				output.write( output_bit_buffer >> 24 );
			}
			catch( IOException ioe )
			{
				System.out.println( "IOException in output_code()" );
				System.exit( 1 );
			}
			output_bit_buffer <<= 8;
			output_bit_count -= 8;
		}
	}
}
308219358.gif
#2

انا شغال معايا تمام

بس اعطيته ملف ISO مساحته590

ضغطه الى 688

انا شغلته باستخدم مسار ثابت للملف

هكذا

        	input = new FileInputStream(new File("d:/c.iso"));
1 −1

Software Developer
Mahmoudkelany.com


 

#3

ما المشكلة بالضبط؟

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#4

أخي يا ريت يكون بالبداية شرح عن الكود

إذا نظــرتَ نيـوب اللـّيـثِ بـارزةً         فـلا تظــنَنَ أَنَّ اللـّيـــثَ يبْتسـِـــمُ


 


 


رسم المخططات البيانية بواسطة الجافا


 


كتاب تحليل وتصميم نظم المعلومات


#5
Mahmoud Kelany كتب:

انا شغال معايا تمام

بس اعطيته ملف ISO مساحته590

ضغطه الى 688

انا شغلته باستخدم مسار ثابت للملف

هكذا

        	input = new FileInputStream(new File("d:/c.iso"));

شكرا لك اخي الكريم

بالبداية اعطيتة ملف من امتداد txt

لهذا لم يعمل معي

علاء الصالحي كتب:

ما المشكلة بالضبط؟

تحياتي

المشكلة انني لم اعرف بداية ما نوع الملف الذي ابعثه عن طريق

المين كلاس

شكرا لاهتمامك

308219358.gif
#6

حسنا ما سبق كان كود لضغط ملف

اما ما يلي فهو ضغط لتكست صغيرة

package lzw;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.*;
import java.util.HashMap.*;


public class LZWCompression {
    private   int R = 0;        // number of input chars
    static HashMap<String,Integer> st = new HashMap<String,Integer>();
    public void createDictionary(String input){
        R=input.length();
        int j=0;
        for (int i = 0; i < R; i++)
            if (!st.containsKey(input.substring(i,i+1))) {
                j++;
                st.put( input.substring(i, i+1),j);
            }
    }
    public String compression(String input) {
          this.createDictionary(input);
        String output="";
        String c=input.substring(1);
        String s =input.substring(0,1);
        String tmp = "";
        int x=0;
         while (c.length()>x){
            tmp = s + c.substring(x, x+1);
             if (!st.containsKey(tmp)){
                output=output+st.get(s).toString();
                st.put(tmp, st.size()+1);
                if(tmp.length()==3){

                }
                s=c.substring(x, x+1);
             }else{  
               s=tmp;
            } 
               x++;
         }
        output+=st.get(c.substring(x-1, x));
        return output;
    }
    public String decompression(String input){
        String output="";
        for (int i=0;i<input.length();i++){
            output+=getKey(st,input.substring(i, i+1).toString());
        }
        return output;
    }
    private static String getKey(HashMap<String,Integer> map, String value) {
       for (Map.Entry<String,Integer> entry : map.entrySet()) {
          if (value.equals(entry.getValue().toString())) {
            return entry.getKey();
          }
        }
        return "No value found";
    }
}

سؤالي هو عن فائدة (عمل) السطر التلي :

static HashMap<String,Integer> st = new HashMap<String,Integer>();
308219358.gif
#7

ابحثي هنا في المنتدى عن العموميات Generics

ويا ريت لو كان كل سؤال في موضوع مختلف

تحياتي

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

مواضيع مشابهة