?? zipper.java
字號:
//這是一個用于壓縮ZIP文件的類//
import java.io.*;
import java.util.zip.*;//java.until提供一組常用的函數的類
class Zipper {
public static boolean verbose = false;
public static int compressionLevel = Deflater.DEFAULT_COMPRESSION;
private String Name;
private ZipOutputStream zip;
private int entryNum;
public Zipper(String fileName){
Name=fileName;
entryNum=0;
}
public String getName(){
return Name;
}
public int getEntryNum(){
return entryNum;
}
public void setName(String newName){
Name=newName;
}
public int init(){
//建立并初始化一個輸出流
try{
zip = new ZipOutputStream(new FileOutputStream(Name));
zip.setComment("zip file created by java");
zip.setMethod(ZipOutputStream.DEFLATED);
zip.setLevel(compressionLevel);
}
catch(Exception e){
System.out.println(e);
return 1;
}
return 0;
}
public int addFile(String fileName){
try{
File file = new File(fileName);
FileInputStream in = new FileInputStream(file);
byte[] bytes = new byte[in.available()];
//in.available可以返回輸入流中可以正常讀取的字節數目,以免阻塞
in.read(bytes);
in.close();
//建立并初始化zipentry
ZipEntry entry = new ZipEntry(file.getName());
entry.setTime(file.lastModified());
// write the entry header, and the data, to the zip
zip.putNextEntry(entry);
zip.write(bytes);
// write the end-of-entry marker to the zip
zip.closeEntry();
}
catch(Exception e){
System.out.println(e);
return 1;
}
entryNum++;
return 0;
}
public int close(){
try{
zip.close();
}
catch(Exception e){
System.out.println(e);
return 1;
}
return 0;
}
public static void main( String[] args ){
boolean verbose = false;
int compressionLevel = Deflater.DEFAULT_COMPRESSION;
int argc = args.length;
int argn;
// 解析命令行參數
for (argn=0; (argn<args.length) && args[argn].charAt(0)=='-'; ++argn){
if ( args[argn].equals( "-v" ) )
verbose = true;
else if ( args[argn].equals( "-l" ) ) {
++argn;
compressionLevel = Integer.parseInt(args[argn]);
}
else{
System.out.println( "usage: Zipper [-v] [-f] [-l level] zipfile files" );
return;
}
}
if ( argc - argn < 1 ){
System.out.println( "usage: Zipper [-v] [-f] [-l level] zipfile files" );
return;
}
else try{
//余下的第一個參數是生成ZIP文件的名稱
Zipper zip=new Zipper(args[argn++]);
zip.verbose=verbose;
zip.compressionLevel=compressionLevel;
zip.init();
//其余的參數是需要壓縮的文件的名稱
for ( ; argn < args.length; ++argn )
//將一個文件讀入內存
zip.addFile(args[argn]);
zip.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -