这是本人第一次发文章,不足之处请多多指教!!!
/**
*功能: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();
}
}
/**
*生成文件目录
*根据show的值来确定返回值
*如果show的值为"sh"则返回在屏幕上显示的名称
*如果show的值为"gr"则返回实际名称
*/
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;
}
}
/**
*读取文件内容
*根据传递进来的zip文件对象和
*zip中所要显示的文件
*用户根据type来选择返回的是unicode信息还字节信息
*如果type是"str"则返回字符串信息,"byte"则返回通过字节获得的文件内容
*/
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 + "\n\r";
}
}
}
}
//关闭文件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();
}
}
}
文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




