*功能:Zip文件释放,Zip文件目录、选择目录后显示文件内容
* 首先用户输入zip文件路径及名称,读取zip文件将文件集中的文件清单显示出来,
* 用户选择需要显示的文件,之后通过DataInputStream类将文件内容读取出来并
* 显示在标准输出端
*日期:2005-06-27
*作者:Pcera
*/
import java.util.*;
import java.util.zip.*;
import java.io.*;
class ZipFileHandle{
private String[] FileNameArray; //真实文件名存放数组
private String[] FileNameArrayShow; //需要显示的文件名存放数组
private ZipInputStream zipFile; //zip输入流对象
private ZipEntry entry; //zip文件入口对象
private int zipFileCount = 0; //zip中的文件总数
public ZipFileHandle(File file){
try{
while (!(file.exists())) {
System.out.println("Plase input right path again: ");
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
String filepath = userInput.readLine();
file = new File(filepath);
}
zipFile = new ZipInputStream(new FileInputStream(file));
while ((zipFile.getNextEntry()) != null){
zipFileCount++;
}
FileNameArray = new String[zipFileCount];
FileNameArrayShow = new String[zipFileCount];
}catch(IOException e){
System.out.println("初始化错误!!!");
e.printStackTrace();
}
}
public String[] getFileNameList(String show,File file){
try{
int i = 0;
String FileName;
zipFile = new ZipInputStream(new FileInputStream(file));
while ((entry = zipFile.getNextEntry()) != null){
FileName = entry.getName();
//真实文件名附值
FileNameArray[i] = FileName;
//显示文件名附值
if (FileName.equals("")) FileName = "...";
FileName = Integer.toString(i) + "-|" + FileName;
FileNameArrayShow[i] = FileName;
i++;
}
zipFile.close();
//根据条件返回文件数组
if(show == "sh"){
return FileNameArrayShow;
}else{
return FileNameArray;
}
}catch(IOException e){
System.out.println("读取zip文件内的文件名出错!!!");
e.printStackTrace();
return null;
}
}
public String loadFileCon(String fileName,File file,String type){
String fileContent = "",conTemp = "";
byte[] fileConfByte;
try{
//找到要显示的文件入口,然后读取通过文本格式读取文件内容
zipFile = new ZipInputStream(new FileInputStream(file));
//读取文件内容
while ((entry = zipFile.getNextEntry()) != null){
if (entry.getName().equals(fileName)){
//通过字节读取文件内容
if (type.equals("Byte")){
fileConfByte = new byte[(int)entry.getSize()];
DataInputStream reader = new DataInputStream(zipFile);
reader.readFully(fileConfByte,0,(int)entry.getSize());
fileContent = new String(fileConfByte);
}else if ((type.equals("Str"))){
//通过unicode字符读取文件内容
BufferedReader in = new BufferedReader(new InputStreamReader(zipFile));
while ((conTemp = in.readLine()) != null){
fileContent = fileContent + conTemp + "";
}
}
}
}
//关闭文件zip流
zipFile.closeEntry();
zipFile.close();
//返回
return fileContent;
}catch(IOException e){
System.out.println("读取文件内容失败!!!");
e.printStackTrace();
return null;
}
}
public static void main(String args[]){
String filePath;
String[] showFileArr;
String[] realFileArr;
String fileContent;
int choose;
//通过用户获得文件路径和文件名
try{
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Plase zip file path and name !!!");
filePath = userInput.readLine();
File file = new File(filePath);
while (!(file.exists())) {
System.out.println("Plase input right path again: ");
filePath = userInput.readLine();
file = new File(filePath);
}
//显示文件目录,显示选择的文件的内容
//获得真实文件名和显示文件名数组
ZipFileHandle zipFile = new ZipFileHandle(file);
showFileArr = zipFile.getFileNameList("sh",file);
realFileArr = zipFile.getFileNameList("gr",file);
//打印显示文件名数组
int i = 0;
while (i < showFileArr.length){
System.out.println(showFileArr[i]);
i++;
}
//通过用户输入获得需要显示的文件
System.out.println("Plase choose file num!!!");
choose = Integer.parseInt(userInput.readLine());
if ((choose <0)||(choose > showFileArr.length)){
System.out.println("Plase choose file num retry!!!");
choose = Integer.parseInt(userInput.readLine());
}
//获得用户选择的文件的内容
fileContent = zipFile.loadFileCon(realFileArr[choose],file,"Byte");
System.out.println(fileContent);
}catch(Exception e){
System.out.println("测试程序出错!!!");
e.printStackTrace();
}
}
}