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


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

Posted by ±è´öÅ on July 25, 1997 at 04:52:56:

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


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


ÀÌ ÇÁ·Î±×·¥ÀÌ ¾ÐÃàÇÑ È­ÀÏÀ» WinZipÀÌ Á¦´ë·Î ó¸®ÇÏÁö ¸øÇÑ´Ù¸é,
°®°í °è½Å WinZip ÇÁ·Î±×·¥ ¹öÀü¿¡ ¹ö±×°¡ Àֱ⠶§¹®ÀÏ °ÍÀÔ´Ï´Ù.
http://www.winzip.com ¿¡¼­ ÃֽŠ¹öÀü (¹öÀü 6.3 Beta 4)À»
´Ù¿î·Îµå¹ÞÀ¸½Ç ¼ö ÀÖ½À´Ï´Ù.


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


----------------- ZipTest.java --------------------------------------
// Usage: java ZipTest [ -r ] zipfile file1 [ file2 ... ]
import java.io.*;
import java.util.zip.*;


class ZipTest
{
static boolean isRecursive = false;
static ZipOutputStream zos;


public static void main( String[] args )
throws IOException
{
try
{ int argnum = 0;
if ( args[argnum].equals("-r") )
{ isRecursive = true;
argnum++;
}
File zipfile = new File( args[argnum++] );
if ( zipfile.exists() )
throw new Error( "ZIP È­ÀÏ " + zipfile + "ÀÌ ÀÌ¹Ì Á¸ÀçÇÔ." );
zos = new ZipOutputStream( new FileOutputStream( zipfile ) );
if ( args.length <= argnum )
throw new Error(
"»ç¿ë¹ý: java ZipTest [ -r ] zipfile file1 [ file2 ... ]" );
for(int i = argnum; i < args.length; ++i)
outputEntry( new File(args[i]) );
zos.close();
} catch( Error ex )
{ System.err.println( "¿À·ù: " + ex.getMessage() );
}
}

public static void outputEntry( File f ) throws Error, IOException
{ if ( ! f.exists() )
throw new Error( "È­ÀÏ " + f + "ÀÌ Á¸ÀçÇÏÁö ¾ÊÀ½" );
String adjustedPath = f.getPath().replace(File.separatorChar, '/');
if ( f.isDirectory() && ! adjustedPath.endsWith("/") )
adjustedPath += '/';
ZipEntry entry = new ZipEntry( adjustedPath );
entry.setTime( f.lastModified() );
// File Ŭ·¡½º¿¡¼­ lastModified() ¸Þ½îµåÀÇ ½Ã°¢ ±âÁØÀÌ ¹®¼­È­µÇ¾î
// ÀÖÁö´Â ¾ÊÀ¸³ª, ¼Ö¶ó¸®½º¿Í À©µµ¿ìÁî¿¡¼­´Â Date Ŭ·¡½º¿Í °°Àº
// ½Ã°¢ ±âÁØÀÌ »ç¿ëµÇ°í ÀÖÀ½.
zos.putNextEntry( entry );

if ( f.isDirectory() )
{ System.out.println( "µð·ºÅ丮 ÷°¡: " + f );
if ( isRecursive )
{ String[] files = f.list();
for( int i = 0; i < files.length; ++i )
outputEntry( new File(f, files[i]) ); // Àç±ÍÀû È£Ãâ »ç¿ë
}
} else
{ System.out.println( " È­ÀÏ Ã·°¡: " + f );
FileInputStream fis = new FileInputStream( f );
byte buf[] = new byte[1024];
for( int cnt; (cnt = fis.read(buf)) != -1; )
zos.write( buf, 0, cnt );
fis.close();
}
}


static class Error extends Exception
{ public Error(String mesg)
{ super(mesg); }
}
}
---------------------------------------------------------------------------------



----------------- UnzipTest.java --------------------------------------
// Usage: java UnzipTest [ -l ] zipfile
import java.io.*;
import java.util.zip.*;
import java.util.*;
import java.text.*;


class UnzipTest
{
public static void main( String[] args )
throws IOException
{
try
{
boolean isListing = false;
int argnum = 0;
if ( args[0].equals("-l") )
{ isListing = true;
argnum++;
}
File zipfile = new File( args[argnum] );
if ( ! zipfile.exists() )
throw new Error( "ZIP È­ÀÏ " + zipfile + "ÀÌ Á¸ÀçÇÏÁö ¾ÊÀ½." );
ZipInputStream zis
= new ZipInputStream( new FileInputStream( zipfile ) );
if ( isListing )
list(zis);
else
extract(zis);
System.out.close();
} catch( Error ex )
{ System.err.println( "¿À·ù: " + ex.getMessage() );
}
}


static void list(ZipInputStream zis) throws IOException
{
ZipEntry entry;
System.out.println( " Å©±â (¾ÐÃà Å©±â) ³¯Â¥ ½Ã°¢ À̸§" );
System.out.println( " ---------------- ---- ---- ----" );
MessageFormat fmt = new MessageFormat(
"{0,date,MM-dd-yy} {0,time,HH:mm} {1}" );
while( (entry = zis.getNextEntry()) != null )
{ zis.closeEntry();
System.out.println(
padToWidth( String.valueOf(entry.getSize()), 7 )
+ padToWidth( "(" + String.valueOf(entry.getCompressedSize())
+ ")", 10 )
+ " " + fmt.format( new Object[] {
new Date( entry.getTime() ),
entry.getName() } )
);
}
}


static String padToWidth(String s, int width)
{ if ( s.length() >= width )
return s;
char padded[] = new char[width - s.length()];
for( int i = 0; i < padded.length; i++ )
padded[i] = ' ';
return padded + s;
}


static void extract(ZipInputStream zis) throws IOException, Error
{
ZipEntry entry;
while( (entry = zis.getNextEntry()) != null )
{ File entryFile
= new File( entry.getName().replace('/', File.separatorChar) );
if ( entry.isDirectory() )
{ if ( ! entryFile.exists() )
{ System.out.println( " µð·ºÅ丮 »ý¼º: " + entryFile );
entryFile.mkdirs();
}
continue;
}


if ( entryFile.getParent() != null )
{ File parent = new File( entryFile.getParent() );
if ( ! parent.exists() )
parent.mkdirs();
}
if ( entry.getMethod() == ZipEntry.STORED )
{ System.out.println( " ̧̉: " + entryFile );
} else if ( entry.getMethod() == ZipEntry.DEFLATED )
{ System.out.println( " ¾ÐÃà Ç®±â: " + entryFile );
} else
throw new Error( entryFile
+ "È­ÀÏ¿¡¼­ ó¸®ÇÒ ¼ö ¾ø´Â ¾ÐÃà ¹æ½ÄÀÌ »ç¿ëµÇ¾úÀ½" );
if ( entryFile.exists() )
throw new Error( entryFile + "ÀÌ ÀÌ¹Ì Á¸ÀçÇÔ" );
FileOutputStream fos = new FileOutputStream( entryFile );
byte buf[] = new byte[1024];
for( int cnt; (cnt = zis.read(buf)) != -1; )
fos.write( buf, 0, cnt );
fos.close();
}
}


static class Error extends Exception
{ public Error(String mesg)
{ super(mesg); }
}
}
---------------------------------------------------------------------------------



----------- ½ÇÇà °á°ú ------------------------
C:\example\zip> mkdir test
C:\example\zip> copy *.java test
C:\example\zip> dir test
GZIPT~66 JAV 2,333 97-07-24 3:48 GZIPTest.java
UNZIP~U2 JAV 3,637 97-07-24 3:53 UnzipTest.java
ZIPTE~7G JAV 2,479 97-07-24 3:27 ZipTest.java
CHECK~Z6 JAV 1,674 97-07-22 6:11 CheckedStreamTest.java
C:\example\zip> java -Duser.timezone=JST ZipTest -r test.zip test
-------------------> workaround for JDK1.1 timezone bug
µð·ºÅ丮 ÷°¡: test
È­ÀÏ Ã·°¡: test\GZIPTest.java
È­ÀÏ Ã·°¡: test\CheckedStreamTest.java
È­ÀÏ Ã·°¡: test\ZipTest.java
È­ÀÏ Ã·°¡: test\UnzipTest.java
C:\example\zip> java -Duser.timezone=JST UnzipTest -l test.zip
Å©±â (¾ÐÃà Å©±â) ³¯Â¥ ½Ã°¢ À̸§
---------------- ---- ---- ----
0 (2) 07-25-97 01:15 test/
2333 (785) 07-24-97 03:48 test/GZIPTest.java
3637 (1243) 07-24-97 03:53 test/UnzipTest.java
2479 (1027) 07-24-97 03:27 test/ZipTest.java
1674 (586) 07-22-97 06:11 test/CheckedStreamTest.java
C:\example\zip> move test test2
C:\example\zip> java UnzipTest test.zip
µð·ºÅ丮 »ý¼º: test\
¾ÐÃà Ç®±â: test\GZIPTest.java
¾ÐÃà Ç®±â: test\UnzipTest.java
¾ÐÃà Ç®±â: test\ZipTest.java
¾ÐÃà Ç®±â: test\CheckedStreamTest.java


(WinZip ÇÁ·Î±×·¥°úÀÇ È£È¯¼º Å×½ºÆ®)
C:\example\zip> move test test3
(WinZip¿¡¼­ test.zipÀ» ExtractÇÑ´Ù)
C:\example\zip> dir test
GZIPT~66 JAV 2,333 97-07-24 3:48 GZIPTest.java
UNZIP~U2 JAV 3,637 97-07-24 3:53 UnzipTest.java
ZIPTE~7G JAV 2,479 97-07-24 3:27 ZipTest.java
CHECK~Z6 JAV 1,674 97-07-22 6:11 CheckedStreamTest.java
C:\example\zip> move test test4


(WinZip¿¡¼­ test2.zipÀ» »ý¼ºÈÄ, test2\*.*¸¦ ÷°¡)
C:\example\zip> java -Duser.timezone=JST UnzipTest -l test2.zip
Å©±â (¾ÐÃà Å©±â) ³¯Â¥ ½Ã°¢ À̸§
---------------- ---- ---- ----
2333 (785) 07-24-97 03:48 GZIPTest.java
3637 (1253) 07-24-97 03:53 UnzipTest.java
2479 (1031) 07-24-97 03:27 ZipTest.java
1674 (586) 07-22-97 06:11 CheckedStreamTest.java
C:\example\zip> mkdir test
C:\example\zip> cd test
C:\example\zip\test> set CLASSPATH=..
C:\example\zip\test> java UnzipTest ..\test2.zip
¾ÐÃà Ç®±â: GZIPTest.java
¾ÐÃà Ç®±â: UnzipTest.java
¾ÐÃà Ç®±â: ZipTest.java
¾ÐÃà Ç®±â: CheckedStreamTest.java
C:\example\zip\test> dir *.*
GZIPT~66 JAV 2,333 97-07-25 1:28 GZIPTest.java
UNZIP~U2 JAV 3,637 97-07-25 1:28 UnzipTest.java
ZIPTE~7G JAV 2,479 97-07-25 1:28 ZipTest.java
CHECK~Z6 JAV 1,674 97-07-25 1:28 CheckedStreamTest.java


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


Follow Ups:



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

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


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