.gz ¾ÐÃà È­ÀÏ Ã³¸® ¹æ¹ý (java.util.zip ÆÐÅ°Áö)


[ Follow Ups ] [ Post Followup ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]

Posted by ±è´öÅ on July 25, 1997 at 04:47:20:

´ÙÀ½ ÇÁ·Î±×·¥Àº ¾ÐÃà È­ÀÏÀ» Áö¿øÇÏ´Â java.util.zip ÆÐÅ°Áö¿¡¼­
.gz ¾ÐÃà È­ÀÏ Çü½Ä (´ÜÀÏ È­ÀÏ ¾ÐÃà Çü½Ä¿¡ ¼ÓÇÔ)À» ó¸®Çϴ Ŭ·¡½ºÀÇ
»ç¿ë¹ýÀ» º¸¿©ÁÝ´Ï´Ù.
µ¡ºÙ¿©, File Ŭ·¡½º¸¦ »ç¿ëÇÏ¿© È­ÀÏÀ» ´Ù·ç´Â ¹æ¹ý,
¹öÆÛ (buf[1024])¸¦ »ç¿ëÇÑ È­ÀÏ ÀÔÃâ·Â ¼Óµµ Çâ»ó ¹æ¹ý,
¿¹¿Ü Ŭ·¡½º, ÁßøµÈ Ŭ·¡½º¸¦ »ç¿ëÇÏ´Â ¹æ¹ýµµ º¸¿©ÁÝ´Ï´Ù.
ÇöÀç, ÀÌ ÆÐÅ°ÁöÀÇ ÀÌÇظ¦ µ½´Â ¹®¼­¿Í ¿¹Á¦°¡ ¾ø´Â °Í °°¾Æ °£´ÜÇÏ°Ô
¸¸µé¾î º¸¾Ò½À´Ï´Ù.


ÀÌ ÇÁ·Î±×·¥Àº gzip ÇÁ·Î±×·¥°ú °°Àº ÀÏÀ» Çϵµ·Ï ¸¸µé¾îÁ® ÀÖ½À´Ï´Ù¸¸,
ÀÌ ÇÁ·Î±×·¥¿¡¼­ »ç¿ëµÈ FileOutputStreamÀ̳ª FileInputStreamÀ»
URL ȤÀº Socket °´Ã¼·ÎºÎÅÍ ¾òÀº InputStream ȤÀº OutputStreamÀ¸·Î
¹Ù²Ù¾îÁÖ¸é ³×Æ®¿öÅ©»ó¿¡¼­ ¾ÐÃàÇÑ ÇüÅ·ΠÀڷḦ Àü¼ÛÇÏ°í ¹Þ¾Æ¼­
ó¸®ÇÏ¿©, ³×Æ®¿öÅ© Àü¼Û ¼Óµµ¸¦ Çâ»ó½Ãų ¼ö ÀÖ½À´Ï´Ù.
ÀÏ¹Ý ÅؽºÆ® ÀڷḦ ¾ÐÃàÇϸé 2 ~ 4¹è·Î ¾ÐÃàµÈ´Ù°í ¾Ë·ÁÁ® ÀÖ½À´Ï´Ù.
¶ÇÇÑ, ÀÌ Å¬·¡½ºµéÀº üũ½æÀ» »ç¿ëÇÏ¿© ÀúÀå, Àü¼Û ¿À·ù¸¦ °Ë»çÇØÁÝ´Ï´Ù.


- Å×½ºÆ®µÈ ȯ°æ: JDK1.1.3 + ¼Ö¶ó¸®½º, JDK1.1.3 + À©µµ¿ìÁî


---------------------- GZIPTest.java -------------------------------
// Usage: java GZIPTest [ -d ] infile
import java.io.*;
import java.util.zip.*;


class GZIPTest
{
public static void main( String[] args )
throws IOException
{
try
{ if ( ! args[0].equals("-d") )
gzip( args[0] );
else
gunzip( args[1] );
} catch( Error ex )
{ System.err.println( "¿À·ù: " + ex.getMessage() );
}
}


static void gzip( String fname ) throws Error, IOException
{ File infile = new File( fname );
File outfile = new File( fname + ".gz" );
if ( outfile.exists() )
throw new Error( "È­ÀÏ " + outfile + "ÀÌ ÀÌ¹Ì Á¸ÀçÇÕ´Ï´Ù." );
FileInputStream fis = new FileInputStream( infile );
FileOutputStream fos = new FileOutputStream( outfile );
GZIPOutputStream gzos = new GZIPOutputStream( fos );
byte buf[] = new byte[1024];
for( int cnt; (cnt = fis.read(buf)) != -1; )
gzos.write( buf, 0, cnt );
gzos.close();
fis.close();
infile.delete();
}


static void gunzip( String fname ) throws Error, IOException
{ File infile = new File( fname );
File outfile;
if ( fname.endsWith(".gz") )
outfile = new File( fname.substring(0, fname.length() - 3 ) );
else if ( fname.endsWith(".z") )
outfile = new File( fname.substring(0, fname.length() - 2 ) );
else
throw new Error( "¾ÐÃà È­ÀÏ À̸§ÀÌ .gz³ª .z·Î ³¡³ªÁö ¾Ê¾ÒÀ½" );
if ( outfile.exists() )
throw new Error( "È­ÀÏ " + outfile + "ÀÌ ÀÌ¹Ì Á¸ÀçÇÔ" );
try
{
FileInputStream fis = new FileInputStream( infile );
GZIPInputStream gzis = new GZIPInputStream( fis );
FileOutputStream fos = new FileOutputStream( outfile );
byte buf[] = new byte[1024];
for( int cnt; (cnt = gzis.read(buf)) != -1; )
fos.write( buf, 0, cnt );
fos.close();
gzis.close();
infile.delete();
} catch( ZipException ex)
{ throw new Error( "ZIP È­ÀÏ " + infile + "ÀÌ ¼Õ»óµÇ¾úÀ½" );
}
}


// ÁßøµÈ Ŭ·¡½º (³»ºÎ Ŭ·¡½º¿Í ´Ù¸§)
static class Error extends Exception
{ public Error(String mesg)
{ super(mesg); }
}
}
------------------------------------------------------------------------



C:\example\zip> dir gzip.*
gzip txt 1,672 97-07-22 1:06 gzip.txt
C:\example\zip> java GZIPTest gzip.txt
GZIPT~GN GZ 914 97-07-22 2:25 gzip.txt.gz
C:\example\zip> dir gzip.*
GZIPT~GN GZ 914 97-07-22 2:25 gzip.txt.gz
C:\example\zip> java GZIPTest -d gzip.txt.gz
C:\example\zip> dir gzip.*
gzip txt 1,672 97-07-22 2:25 gzip.txt


(gzip ÇÁ·Î±×·¥°úÀÇ È£È¯¼º °Ë»ç)
C:\example\zip> java GZIPTest gzip.txt
C:\example\zip> gzip -d gzip.txt.gz
C:\example\zip> dir gzip.*
gzip txt 1,672 97-07-22 2:28 gzip.txt
C:\example\zip> gzip gzip.txt
C:\example\zip> java GZIPTest -d gzip.txt.gz
C:\example\zip> dir gzip.*
gzip txt 1,672 97-07-22 2:29 gzip.txt


--
Deogtae Kim (±è´öÅÂ)
CA Lab. CS Dept. KAIST



Follow Ups:



À̾ ±Û¿Ã¸®±â(´äÇϱâ)

À̸§:
E-Mail:
Á¦¸ñ:
³»¿ë:
°ü·Ã URL(¼±ÅÃ):
URL Á¦¸ñ(¼±ÅÃ):
°ü·Ã À̹ÌÁö URL:


[ Follow Ups ] [ Post Followup ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]