📄 例9-8.java
字号:
//Example 9.8
import java.util.*;
import java.util.zip.*;
import java.io.*;
public class tt
{
static String zipName,fileName,entryName;
static byte[] buffer=new byte[1024];
static int bytesRead;
public static void main(String args[]) throws IOException
{
boolean HELP=false;
if(args.length==0)
{
help();System.exit(0);
}
if((args.length>2)&&args[0].equals("c"))
{ HELP=true;
zipName=args[1];
try
{
ZipOutputStream zip=
new ZipOutputStream(new FileOutputStream(zipName));
for(int i=2;i<args.length;i++)
{
fileName=args[i];
FileInputStream file=new FileInputStream(fileName);
ZipEntry entry=new ZipEntry(fileName);
zip.putNextEntry(entry);
while((bytesRead=file.read(buffer))!=-1)
zip.write(buffer,0,bytesRead);
System.out.println(entry.getName()+"added.");
file.close();
}
zip.close();
System.out.println(zipName+" created.");
}
catch(IOException e){ System.out.println(e);}
}
if(args.length==3&&args[0].equals("x"))
{
HELP=true; zipName=args[1];entryName=args[2];
try
{
ZipFile zip=new ZipFile(zipName);
ZipEntry entry=new ZipEntry(entryName);
if(entry!=null)
{
InputStream entryStream=zip.getInputStream(entry);
FileOutputStream file=new FileOutputStream(entry.getName());
while((bytesRead=entryStream.read(buffer))!=-1)
{
file.write(buffer,0,bytesRead);
}
System.out.println(entry.getName()+"extracted.");
file.close();entryStream.close();
}
else
System.out.println(entryName+" not found.");
zip.close();
}
catch(IOException e){ System.out.println(e); }
}
if(args.length==2&&args[0].equals("l"))
{
HELP=true;
try
{
ZipFile zip=new ZipFile(args[1]);
for(Enumeration list=zip.entries();list.hasMoreElements();)
{
ZipEntry entry=(ZipEntry)list.nextElement();
System.out.println(entry.getName());
}
zip.close();
}
catch(IOException e){ System.out.println(e); }
}
if(!HELP) help();
}
private static void help()
{
System.out.println("Usage:Java tt <command> <zip_file>[<file>..]");
System.out.println(" ");
System.out.println("<command>:");
System.out.println("c:Create a zip file.");
System.out.println("x:Extract files from a zip file.");
System.out.println("l:List context of a zip file.");
System.out.println("Examples:");
System.out.println("java tt c myzip.zip t01.txt t02.txt");
System.out.println("java tt x myzip.zip t01.txt");
System.out.println("java tt l myzip.zip");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -