大家帮我看看我的程序到底哪里有问题?我的解压程序只能解压到我.zip文件所在的磁盘,不能解压到其它的磁盘?比如我的 e:\aa.zip只能解压到e:\下,不能解压到其它盘比如c:,d:等,这是怎么回事啊?程序如下:
package com.javaftp;
import java.io.*;import java.util.zip.*;
public class UnZip {
static final int BUFFER = 2048;public static void main(String argv[]) {
try {
String fileName = "e:\\aa.zip"; //待解压缩的文件名
/*
* 创建一个File实例file
public class File 为文件和目录路径名的抽象表示形式。
构造方法File(String pathname)
通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。
*/
File file = new File(fileName);
//从文件名中提取出子目录(路径)名
//lastIndexOf(String str)返回在此字符串中最右边出现的指定子字符串的索引
int n=fileName.lastIndexOf(".");
String folderName = fileName.substring(0,n);
System.out.println(n);
File folder = new File(folderName);
folder.mkdir(); //创建子目录(路径)
System.out.println(folderName);
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(file);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
//ZipEntry类用于表示 ZIP 文件条目
ZipEntry entry;
// getNextEntry() 读取下一个 ZIP 文件条目并将流定位到该条目数据的开始处
while((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory()) continue;
System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
/*write the files to the disk
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流。
*/ File f1 = new File(entry.getName());
if (f1.getParent()!=null){
String subDirName=folderName + "\\" + f1.getParent();
File subDir = new File(subDirName);
System.out.println(subDir.getAbsolutePath());
subDir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(folderName + "\\" + entry.getName());
/*BufferedOutputStream(OutputStream out, int size)
创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的基础输出流。
*/
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
//刷新此缓冲的输出流。
dest.flush();
dest.close();
}
zis.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
我这个程序只能解压到e:\下,怎么解压到c:\AA目录下呢?
我觉得是 FileOutputStream fos = new FileOutputStream(folderName + "\\" + entry.getName());的问题,可我改不好!大家帮帮我,怎么改啊!!我郁闷了很久了!大家测试的时候一定要拿多层目录测试!大家最好把修改后的代码帖上来!谢拉!
package com.javaftp;
import java.io.*;import java.util.zip.*;
public class UnZip {
static final int BUFFER = 2048;public static void main(String argv[]) {
try {
String fileName = "e:\\aa.zip"; //待解压缩的文件名
/*
* 创建一个File实例file
public class File 为文件和目录路径名的抽象表示形式。
构造方法File(String pathname)
通过将给定路径名字符串转换成抽象路径名来创建一个新 File 实例。
*/
File file = new File(fileName);
//从文件名中提取出子目录(路径)名
//lastIndexOf(String str)返回在此字符串中最右边出现的指定子字符串的索引
int n=fileName.lastIndexOf(".");
String folderName = fileName.substring(0,n);
System.out.println(n);
File folder = new File(folderName);
folder.mkdir(); //创建子目录(路径)
System.out.println(folderName);
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(file);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
//ZipEntry类用于表示 ZIP 文件条目
ZipEntry entry;
// getNextEntry() 读取下一个 ZIP 文件条目并将流定位到该条目数据的开始处
while((entry = zis.getNextEntry()) != null) {
if (entry.isDirectory()) continue;
System.out.println("Extracting: " +entry);
int count;
byte data[] = new byte[BUFFER];
/*write the files to the disk
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流。
*/ File f1 = new File(entry.getName());
if (f1.getParent()!=null){
String subDirName=folderName + "\\" + f1.getParent();
File subDir = new File(subDirName);
System.out.println(subDir.getAbsolutePath());
subDir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(folderName + "\\" + entry.getName());
/*BufferedOutputStream(OutputStream out, int size)
创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的基础输出流。
*/
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
//刷新此缓冲的输出流。
dest.flush();
dest.close();
}
zis.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
我这个程序只能解压到e:\下,怎么解压到c:\AA目录下呢?
我觉得是 FileOutputStream fos = new FileOutputStream(folderName + "\\" + entry.getName());的问题,可我改不好!大家帮帮我,怎么改啊!!我郁闷了很久了!大家测试的时候一定要拿多层目录测试!大家最好把修改后的代码帖上来!谢拉!
解决方案 »
- 【SOS】怎么产生1-100之间的100个不重复的随机整数才能效率高?
- 如何根据使用JDK的版本导入不同的包?
- java如何能读取Win7的mac地址
- 24点用JAVA实现
- 拜托大家帮帮忙看看?怎么老是报异常?
- 大家看我报的错吧!!!
- 在运行 Java 程序时, 如何隐藏 dos 背景呢?
- 怎么设置ACTION的快捷键?就是CTRL+“字母”的?
- 如何让Jbuilder连接Oracle
- instanceof 内部实现机理??————————————————————————
- 来个经典题目,大家写算法
- 得到了des的密钥就等于可以由加密后的内容得到加密前的内容,是吗?为什么des加密方法还被广泛应用呢?谢谢!
String path = "c:\\01\\source\\";
String folderName = path + fileName.substring(3,n);
substring里取3到n才对!
trumplet(检查)大哥,因为这个程序没有经过严格测试,我总觉得有问题!我在网上发现一个瞒好的解压缩程序,但是感觉它还有些问题,好象也不支持多层目录的,但是它写的挺好!您看下吧:要能把这个程序改好,那就完美了!我测试感觉有问题!
/**
*功能: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();
}
}
}
folder.mkdir(); //创建子目录(路径)
我把上面两句话去了,程序仍然是好的!
我觉得下面这段已经把目录给建好了啊!
if (f1.getParent()!=null){
String subDirName=folderName + "\\" + f1.getParent();
File subDir = new File(subDirName);
System.out.println(subDir.getAbsolutePath());
subDir.mkdirs();
}
为什么又用下面的:
File folder = new File(folderName);
folder.mkdir(); //创建子目录(路径)trumplet(检查),你觉得呢??
还有一个问题,我解压缩程序的中文问题至今未解决!我找不到支持中文的zipinputstream包,能帮帮我吗?