import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.*;
public class GZIPfile {
private boolean flag = true;
//定义一个接口,通过结构来调用该类的方法
public static GZIPfile getInterface(){
return new GZIPfile();
}
//创建一个方法,对文件进行解压缩
public boolean openFile(String InfileName,String OutfileName){
/**
*解压程序
* @InfileName 传入方法的文件名称及文件所在路径的具体值
* @OutfileName 对文件解压缩成功后,要将文件保存的具体位置和名称
* @return 返回类型为boolean,标识文件是否正常操作完成
*/
try {
GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(InfileName));
FileOutputStream out = new FileOutputStream(OutfileName);
byte[] bt = new byte[1024];
int length = 0;
while((length=gzip.read(bt))>0){
out.write(bt, 0, length);
}
} catch (Exception e) {
this.flag = false;
System.out.println(e.getMessage());
}
return flag;
}
//创建一个方法对读取到的文件进行压缩处理 public boolean compFile(String InfileName,String OutfileName){
/*
* @InfileName 读入文件的具体名称和地址的值,对文件的数据进行压缩处理
* @OutfileName 读出的文件要存放的具体的地址和文件名称,处理后的压缩文件
* @return 返回一个boolean值表明程序是否能够争取的处理
*/ try {
GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream(OutfileName));
FileInputStream in = new FileInputStream(InfileName);
byte[] bt = new byte[1024];
int length = 0;
while((length=in.read(bt))>0){
gzip.write(bt, 0, length);
}
} catch (Exception e) {
flag = false;
System.out.println(e.getMessage());
}
return flag;
}
public static void main(String args[]){
GZIPfile zipit = new GZIPfile();
GZIPfile yyhkai = new GZIPfile();
//unexpected end ZLIB input stream出现,注释掉就没问题了
String FileName="c:/multiple/";
File f=new File(FileName);
String s[] = f.list();
for(int i=0;i<s.length;i++){
//String ss = "c:/multiple/"+s[i];//路径要具体到文件而非文件夹
String ss = "d:/data/leno.err";
String ss1 = "d:/me/yyhja.zip";
zipit.compFile(ss,ss1);
yyhkai.openFile("d:/me/yyhja.zip","c:/yyhja");//ZIP读不了WinRAR文件
/*} catch(IOException e){
System.out.println("IOException"+e);
}*/
}
}
}
编译没问题,运行后这句GZIPfile yyhkai = new GZIPfile()的后面出现Unexpected end of ZLIB input stream
另外如题,yyhja打不开,错误为CRC失败,文件被损坏,怎么回事?请教!
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.*;
public class GZIPfile {
private boolean flag = true;
//定义一个接口,通过结构来调用该类的方法
public static GZIPfile getInterface(){
return new GZIPfile();
}
//创建一个方法,对文件进行解压缩
public boolean openFile(String InfileName,String OutfileName){
/**
*解压程序
* @InfileName 传入方法的文件名称及文件所在路径的具体值
* @OutfileName 对文件解压缩成功后,要将文件保存的具体位置和名称
* @return 返回类型为boolean,标识文件是否正常操作完成
*/
try {
GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(InfileName));
FileOutputStream out = new FileOutputStream(OutfileName);
byte[] bt = new byte[1024];
int length = 0;
while((length=gzip.read(bt))>0){
out.write(bt, 0, length);
}
} catch (Exception e) {
this.flag = false;
System.out.println(e.getMessage());
}
return flag;
}
//创建一个方法对读取到的文件进行压缩处理 public boolean compFile(String InfileName,String OutfileName){
/*
* @InfileName 读入文件的具体名称和地址的值,对文件的数据进行压缩处理
* @OutfileName 读出的文件要存放的具体的地址和文件名称,处理后的压缩文件
* @return 返回一个boolean值表明程序是否能够争取的处理
*/ try {
GZIPOutputStream gzip = new GZIPOutputStream(new FileOutputStream(OutfileName));
FileInputStream in = new FileInputStream(InfileName);
byte[] bt = new byte[1024];
int length = 0;
while((length=in.read(bt))>0){
gzip.write(bt, 0, length);
}
} catch (Exception e) {
flag = false;
System.out.println(e.getMessage());
}
return flag;
}
public static void main(String args[]){
GZIPfile zipit = new GZIPfile();
GZIPfile yyhkai = new GZIPfile();
//unexpected end ZLIB input stream出现,注释掉就没问题了
String FileName="c:/multiple/";
File f=new File(FileName);
String s[] = f.list();
for(int i=0;i<s.length;i++){
//String ss = "c:/multiple/"+s[i];//路径要具体到文件而非文件夹
String ss = "d:/data/leno.err";
String ss1 = "d:/me/yyhja.zip";
zipit.compFile(ss,ss1);
yyhkai.openFile("d:/me/yyhja.zip","c:/yyhja");//ZIP读不了WinRAR文件
/*} catch(IOException e){
System.out.println("IOException"+e);
}*/
}
}
}
编译没问题,运行后这句GZIPfile yyhkai = new GZIPfile()的后面出现Unexpected end of ZLIB input stream
另外如题,yyhja打不开,错误为CRC失败,文件被损坏,怎么回事?请教!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货