java解压缩文件代码 java实现解压缩( 三 )


longarrayLen,flagReturn;
int k = 0;
String tempPath;
//存放zip文件清单的路径
String[] pathArray = new String[zipPathCount];
//删除文件
arrayLen = listFilePath.length;
for(int i=0;i(int)arrayLen;i++){
tempPath = releasePath.replace('\\','/') + listFilePath[i];
flagReturn = deleteFile(tempPath);
if (flagReturn == -2){
//什么都不作
}else if (flagReturn == -3){
pathArray[k++] = tempPath;
}else if (flagReturn == -1){
return -1;
}
}
//删除路径
for(k = k - 1;k=0;k--){
flagReturn = deleteFolder(pathArray[k]);
if (flagReturn == -1) return -1;
}
return 0;
}
/**
*获得zip文件的最上层的文件夹名称
*参数:zip文件路径
*返回值:文件夹名称,如果失败则返回null
*/
public String getZipRoot(String infileA){
String rootName;
try{
FileInputStream tempfile = new FileInputStream(infileA);
ZipInputStream AzipInFile = new ZipInputStream(tempfile);
ZipEntry Aentry;
Aentry = AzipInFile.getNextEntry();
rootName = Aentry.getName();
tempfile.close();
AzipInFile.close();
return rootName;
}catch(IOException e){
e.printStackTrace();
return null;
}
}
/**
*释放流,释放占用资源
*/
protected void closeStream() throws Exception{
fileDataIn.close();
fileDataOut.close();
zipInFile.close();
writeData.flush();
}
/**
*解压函数
*对用户的zip文件路径和解压路径进行判断,是否存在和打开
*在输入解压路径时如果输入"/"则在和zip文件存放的统计目录下进行解压
*返回值:0表示释放成功
*-1 表示您所要解压的文件不存在、
*-2表示您所要解压的文件不能被打开、
*-3您所要释放的路径不存在、
*-4您所创建文件目录失败、
*-5写入文件失败、
*-6表示所要释放的文件已经存在、
*-50表示文件读取异常
*/
public long releaseHandle() throws Exception{
File inFile = new File(inFilePath);
File outFile = new File(releaseFilePath);
String tempFile;
String zipPath;
String zipRootPath;
StringtempPathParent;//存放释放路径
byte[] zipEntryFileData;
//作有效性判断
if (checkFile(inFile) == -1) {
return -1;}
if (checkOpen(inFile) == -1) {
return -2;}
//不是解压再当前目录下时对路径作有效性检验
if (!releaseFilePath.equals("/")){
//解压在用户指定目录下
if (checkFile(outFile) == -1) {
return -3;}
}
//获得标准释放路径
if (!releaseFilePath.equals("/")) {
tempPathParent = releaseFilePath.replace('\\','/')+ "/";
}else{
tempPathParent = inFile.getParent().replace('\\','/')+ "/";
}
//获得zip文件中的入口清单
FileNameArray = getFileList(inFilePath);
//获得zip文件的最上层目录
zipRootPath = getZipRoot(inFilePath);
//
fileDataIn = new FileInputStream(inFilePath);
zipInFile = new ZipInputStream(fileDataIn);
//判断是否已经存在要释放的文件夹
if (zipRootPath.lastIndexOf("/")0 ){
if (checkPathExists(tempPathParent +
zipRootPath.substring(0,zipRootPath.lastIndexOf("/"))) == -1){
return -6;
}
}else{
if (checkPathExists(tempPathParent + zipRootPath) == -1){
return -6;
}
}
//
try{
//创建文件夹和文件
int i = 0;
while ((entry = zipInFile.getNextEntry()) != null){
if (entry.isDirectory()){
//创建目录
zipPath = tempPathParent + FileNameArray[i];
zipPath = zipPath.substring(0,zipPath.lastIndexOf("/"));
if (createFolder(zipPath) == -1){

推荐阅读