首页 > 编程知识 正文

java文件解压和压缩文件(java文件解压和压缩文件解压区别)

时间:2023-12-24 12:11:50 阅读:321566 作者:KVAF

本文目录一览:

JAVA 压缩和序列化

压缩和序列化主要用在数据的存储和传输上,二者都是由IO流相关知识实现,这里统一介绍下。

全部章节传送门:

Java I/O类支持读写压缩格式的数据流,你可以用他们对其他的I/O流进行封装,以提供压缩功能。

GZIP接口比较简单,适合对单个数据流进行压缩,在Linux系统中使用较多。

ZIP格式可以压缩多个文件,而且可以和压缩工具进行协作,是经常使用的压缩方法。

JAR(Java Archive,Java 归档文件)是与平台无关的文件格式,它允许将许多文件组合成一个压缩文件。为 J2EE 应用程序创建的 JAR 文件是 EAR 文件(企业 JAR 文件)。

JAR 文件格式以流行的 ZIP 文件格式为基础。与 ZIP 文件不同的是,JAR 文件不仅用于压缩和发布,而且还用于部署和封装库、组件和插件程序,并可被像编译器和 JVM 这样的工具直接使用。在 JAR 中包含特殊的文件,如 manifests 和部署描述符,用来指示工具如何处理特定的 JAR。

如果一个Web应用程序的目录和文件非常多,那么将这个Web应用程序部署到另一台机器上,就不是很方便了,我们可以将Web应用程序打包成Web 归档(WAR)文件,这个过程和把Java类文件打包成JAR文件的过程类似。利用WAR文件,可以把Servlet类文件和相关的资源集中在一起进行发布。在这个过程中,Web应用程序就不是按照目录层次结构来进行部署了,而是把WAR文件作为部署单元来使用。

一个WAR文件就是一个Web应用程序,建立WAR文件,就是把整个Web应用程序(不包括Web应用程序层次结构的根目录)压缩起来,指定一个.war扩展名。下面我们将第2章的Web应用程序打包成WAR文件,然后发布

要注意的是,虽然WAR文件和JAR文件的文件格式是一样的,并且都是使用jar命令来创建,但就其应用来说,WAR文件和JAR文件是有根本区别的。JAR文件的目的是把类和相关的资源封装到压缩的归档文件中,而对于WAR文件来说,一个WAR文件代表了一个Web应用程序,它可以包含 Servlet、HTML页面、Java类、图像文件,以及组成Web应用程序的其他资源,而不仅仅是类的归档文件。

在命令行输入jar即可查看jar命令的使用方法。

把对象转换为字节序列的过程称为对象的序列化。把字节序列恢复为对象的过程称为对象的反序列化。

对象的序列化主要有两种用途:

java.io.ObjectOutputStream代表对象输出流,它的writeObject(Object obj)方法可对参数指定的obj对象进行序列化,把得到的字节序列写到一个目标输出流中。

java.io.ObjectInputStream代表对象输入流,它的readObject()方法从一个源输入流中读取字节序列,再把它们反序列化为一个对象,并将其返回。

只有实现了Serializable的对象才能被序列化。对象序列化包括如下步骤:

对象反序列化的步骤如下:

创建一个可以可以序列化的对象。

然后进行序列化和反序列化测试。

serialVersionUID: 字面意思上是序列化的版本号,凡是实现Serializable接口的类都有一个表示序列化版本标识符的静态变量。

JAVA序列化的机制是通过判断类的serialVersionUID来验证的版本一致的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID于本地相应实体类的serialVersionUID进行比较。如果相同说明是一致的,可以进行反序列化,否则会出现反序列化版本一致的异常,即是InvalidCastException。

为了提高serialVersionUID的独立性和确定性,强烈建议在一个可序列化类中显示的定义serialVersionUID,为它赋予明确的值。

控制序列化字段还可以使用Externalizable接口替代Serializable借口。此时需要定义一个默认构造器,否则将为得到一个异常(java.io.InvalidClassException: Person; Person; no valid constructor);还需要定义两个方法(writeExternal()和readExternal())来控制要序列化的字段。

如下为将Person类修改为使用Externalizable接口。

transient修饰符仅适用于变量,不适用于方法和类。在序列化时,如果我们不想序列化特定变量以满足安全约束,那么我们应该将该变量声明为transient。执行序列化时,JVM会忽略transient变量的原始值并将默认值(引用类型就是null,数字就是0)保存到文件中。因此,transient意味着不要序列化。

静态变量不是对象状态的一部分,因此它不参与序列化。所以将静态变量声明为transient变量是没有用处的。

如何在java中解压zip和rar文件

java中有zip包,可以使用

public void getZipFiles(String zipFile, String destFolder) throws IOException {

BufferedOutputStream dest = null;

ZipInputStream zis = new ZipInputStream(

new BufferedInputStream(

new FileInputStream(zipFile)));

ZipEntry entry;

while (( entry = zis.getNextEntry() ) != null) {

System.out.println( "Extracting: " + entry.getName() );

int count;

byte data[] = new byte[BUFFER];

if (entry.isDirectory()) {

new File( destFolder + "/" + entry.getName() ).mkdirs();

continue;

} else {

int di = entry.getName().lastIndexOf( '/' );

if (di != -1) {

new File( destFolder + "/" + entry.getName()

.substring( 0, di ) ).mkdirs();

}

}

FileOutputStream fos = new FileOutputStream( destFolder + "/"

+ entry.getName() );

dest = new BufferedOutputStream( fos );

while (( count = zis.read( data ) ) != -1)

dest.write( data, 0, count );

dest.flush();

dest.close();

}

}

rar的只能用第三方api,比如junrar

Java压缩与解压缩问题

/**

*类名:zipFileRelease

*说明:一个zip文件解压类

*介绍:主要的zip文件释放方法releaseHandle()

* 用ZipInputStream类和ZipEntry类将zip文件的入口清单列举出来,然后

* 根据用户提供的输出路径和zip文件的入口进行组合通过DataOutputStream

* 和File类进行文件的创建和目录的创建,创建文件时的文件数据是通过

* ZipInputStream类、ZipEntry类、InputStream类之间的套嵌组合获得的。

*注意:如果zip文件中包含中文路径程序将会抛出异常

*日期:2005-7-1

*作者:Pcera

*/

import java.io.*;

import java.util.*;

import java.util.zip.*;

class zipFileRelease{

private String inFilePath;

private String releaseFilePath;

private String[] FileNameArray; //存放文件名称的数组

private ZipEntry entry;

//

private FileInputStream fileDataIn;

private FileOutputStream fileDataOut;

private ZipInputStream zipInFile;

private DataOutputStream writeData;

private DataInputStream readData;

//

private int zipFileCount = 0; //zip文件中的文件总数

private int zipPathCount = 0; //zip文件中的路径总数

/**

*初始化函数

*初始化zip文件流、输出文件流以及其他变量的初始化

*/

public zipFileRelease(String inpath,String releasepath){

inFilePath = inpath;

releaseFilePath = releasepath;

}

/**

*初始化读取文件流函数

*参数:FileInputStream类

*返回值:初始化成功返回0,否则返回-1

*/

protected long initInStream(ZipInputStream zipFileA){

try{

readData = new DataInputStream(zipFileA);

return 0;

}catch(Exception e){

e.printStackTrace();

return -1;

}

}

/**

*测试文件路径

*参数:zip文件的路径和要释放的位置

*返回值:是两位整数,两位数中的十位代表输入路径和输出路径(1输入、2输出)

* 各位数是代表绝对路径还是相对路径(1绝对、0相对)

* 返回-1表示路径无效

protected long checkPath(String inPath,String outPath){

File infile = new File(inPath);

File infile = new File(outPath);

}

*/

/**

*初始化输出文件流

*参数:File类

*返回值:初始化成功返回0,否则返回-1

*/

protected long initOutStream(String outFileA){

try{

fileDataOut = new FileOutputStream(outFileA);

writeData = new DataOutputStream(fileDataOut);

return 0;

}catch(IOException e){

e.printStackTrace();

return -1;

}

}

/**

*测试文件是否存在方法

*参数:File类

*返回值:如果文件存在返回文件大小,否则返回-1

*/

public long checkFile(File inFileA){

if (inFileA.exists()){

return 0;

}else{

return -1;

}

}

/**

*判断文件是否可以读取方法

*参数:File类

*返回值:如果可以读取返回0,否则返回-1

*/

public long checkOpen(File inFileA){

if(inFileA.canRead()){

return inFileA.length();

}else{

return -1;

}

}

/**

*获得zip文件中的文件夹和文件总数

*参数:File类

*返回值:如果正常获得则返回总数,否则返回-1

*/

public long getFilFoldCount(String infileA){

try{

int fileCount = 0;

zipInFile = new ZipInputStream(new FileInputStream(infileA));

while ((entry = zipInFile.getNextEntry()) != null){

if (entry.isDirectory()){

zipPathCount++;

}else{

zipFileCount++;

}

fileCount++;

}

return fileCount;

}catch(IOException e){

e.printStackTrace();

return -1;

}

}

/**

*读取zip文件清单函数

*参数:File类

*返回值:文件清单数组

*/

public String[] getFileList(String infileA){

try{

ZipInputStream AzipInFile = new ZipInputStream(new FileInputStream(infileA));

//创建数组对象

FileNameArray = new String[(int)getFilFoldCount(infileA)];

//将文件名清单传入数组

int i = 0;

while ((entry = AzipInFile.getNextEntry()) != null){

FileNameArray[i++] = entry.getName();

}

return FileNameArray;

}catch(IOException e){

e.printStackTrace();

return null;

}

}

/**

*创建文件函数

*参数:File类

*返回值:如果创建成功返回0,否则返回-1

*/

public long writeFile(String outFileA,byte[] dataByte){

try{

if (initOutStream(outFileA) == 0){

writeData.write(dataByte);

fileDataOut.close();

return 0;

}else{

fileDataOut.close();

return -1;

}

}catch(IOException e){

e.printStackTrace();

return -1;

}

}

/**

*读取文件内容函数

*参数:File类

*返回值:如果读取成功则返回读取数据的字节数组,如果失败则返回空值

*/

protected byte[] readFile(ZipEntry entryA,ZipInputStream zipFileA){

try{

long entryFilelen;

if (initInStream(zipFileA) == 0){

if ((entryFilelen = entryA.getSize()) = 0){

byte[] entryFileData = new byte[(int)entryFilelen];

readData.readFully(entryFileData,0,(int)entryFilelen);

return entryFileData;

}else{

return null;

}

}else{

return null;

}

}catch(IOException e){

e.printStackTrace();

return null;

}

}

/**

*创建目录函数

*参数:要创建目录的路径

*返回值:如果创建成功则返回0,否则返回-1

*/

public long createFolder(String dir){

File file = new File(dir);

if (file.mkdirs()) {

return 0;

}else{

return -1;

}

}

/**

*删除文件

*参数:要删除的文件

*返回值:如果删除成功则返回0,要删除的文件不存在返回-2

* 如果要删除的是个路径则返回-3,删除失败则返回-1

*/

public long deleteFile(String Apath) throws SecurityException {

File file = new File(Apath.trim());

//文件或路径不存在

if (!file.exists()){

return -2;

}

//要删除的是个路径

if (!file.isFile()){

return -3;

}

//删除

if (file.delete()){

return 0;

}else{

return -1;

}

}

/**

*删除目录

*参数:要删除的目录

*返回值:如果删除成功则返回0,删除失败则返回-1

*/

public long deleteFolder(String Apath){

File file = new File(Apath);

//删除

if (file.delete()){

return 0;

}else{

return -1;

}

}

/**

*判断所要解压的路径是否存在同名文件

*参数:解压路径

*返回值:如果存在同名文件返回-1,否则返回0

*/

public long checkPathExists(String AreleasePath){

File file = new File(AreleasePath);

if (!file.exists()){

return 0;

}else{

return -1;

}

}

/**

*删除zip中的文件

*参数:文件清单数组,释放路径

*返回值:如果删除成功返回0,否则返回-1

*/

protected long deleteReleaseZipFile(String[] listFilePath,String releasePath){

long arrayLen,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;

String tempPathParent; //存放释放路径

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){

closeStream();

deleteReleaseZipFile(FileNameArray,tempPathParent);

return -4;

}

}else{

//读取文件数据

zipEntryFileData = readFile(entry,zipInFile);

//向文件写数据

tempFile = tempPathParent + FileNameArray[i];

//写入文件

if (writeFile(tempFile,zipEntryFileData) == -1){

closeStream();

deleteReleaseZipFile(FileNameArray,tempPathParent);

return -5;

}

}

i++;

}

//释放资源

closeStream();

return 0;

}catch(Exception e){

closeStream();

deleteReleaseZipFile(FileNameArray,tempPathParent);

e.printStackTrace();

return -50;

}

}

/**

*演示函数

*根据用户输入的路径对文件进行解压

*/

public static void main(String args[]) throws Exception {

long flag; //返回标志

String inPath,releasePath;

//获得用户输入信息

BufferedReader userInput = new BufferedReader(

new InputStreamReader(System.in));

System.out.println("请输入zip文件路径:");

inPath = userInput.readLine();

System.out.println("请输入保存路径:");

releasePath = userInput.readLine();

userInput.close();

//执行解压缩

zipFileRelease pceraZip = new zipFileRelease(inPath,releasePath);

flag = pceraZip.releaseHandle();

//出错信息打印

if (flag == 0) System.out.println("释放成功!!!");

if (flag == -1) System.out.println("您所要解压的文件不存在!");

if (flag == -2) System.out.println("您所要解压的文件不能被打开!");

if (flag == -3) System.out.println("您所要释放的路径不存在!");

if (flag == -4) System.out.println("您所创建文件目录失败!");

if (flag == -5) System.out.println("写入文件失败!");

if (flag == -6) System.out.println("文件已经存在!");

if (flag == -50) System.out.println("文件读取异常!");

}

}

怎样用java快速实现zip文件的压缩解压缩

package zip;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Enumeration;

import java.util.zip.CRC32;

import java.util.zip.CheckedOutputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipFile;

import java.util.zip.ZipOutputStream;

import org.apache.commons.lang3.StringUtils;

public class ZipUtil {

/**

 * 递归压缩文件夹

 * @param srcRootDir 压缩文件夹根目录的子路径

 * @param file 当前递归压缩的文件或目录对象

 * @param zos 压缩文件存储对象

 * @throws Exception

 */

private static void zip(String srcRootDir, File file, ZipOutputStream zos) throws Exception

{

if (file == null) 

{

return;

}

//如果是文件,则直接压缩该文件

if (file.isFile())

{

int count, bufferLen = 1024;

byte data[] = new byte[bufferLen];

//获取文件相对于压缩文件夹根目录的子路径

String subPath = file.getAbsolutePath();

int index = subPath.indexOf(srcRootDir);

if (index != -1) 

{

subPath = subPath.substring(srcRootDir.length() + File.separator.length());

}

ZipEntry entry = new ZipEntry(subPath);

zos.putNextEntry(entry);

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

while ((count = bis.read(data, 0, bufferLen)) != -1) 

{

zos.write(data, 0, count);

}

bis.close();

zos.closeEntry();

}

//如果是目录,则压缩整个目录

else 

{

//压缩目录中的文件或子目录

File[] childFileList = file.listFiles();

for (int n=0; nchildFileList.length; n++)

{

childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath());

zip(srcRootDir, childFileList[n], zos);

}

}

}

/**

 * 对文件或文件目录进行压缩

 * @param srcPath 要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径

 * @param zipPath 压缩文件保存的路径。注意:zipPath不能是srcPath路径下的子文件夹

 * @param zipFileName 压缩文件名

 * @throws Exception

 */

public static void zip(String srcPath, String zipPath, String zipFileName) throws Exception

{

if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(zipPath) || StringUtils.isEmpty(zipFileName))

{

throw new ParameterException(ICommonResultCode.PARAMETER_IS_NULL);

}

CheckedOutputStream cos = null;

ZipOutputStream zos = null;

try

{

File srcFile = new File(srcPath);

//判断压缩文件保存的路径是否为源文件路径的子文件夹,如果是,则抛出异常(防止无限递归压缩的发生)

if (srcFile.isDirectory()  zipPath.indexOf(srcPath)!=-1) 

{

throw new ParameterException(ICommonResultCode.INVALID_PARAMETER, "zipPath must not be the child directory of srcPath.");

}

//判断压缩文件保存的路径是否存在,如果不存在,则创建目录

File zipDir = new File(zipPath);

if (!zipDir.exists() || !zipDir.isDirectory())

{

zipDir.mkdirs();

}

//创建压缩文件保存的文件对象

String zipFilePath = zipPath + File.separator + zipFileName;

File zipFile = new File(zipFilePath);

if (zipFile.exists())

{

//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException

SecurityManager securityManager = new SecurityManager();

securityManager.checkDelete(zipFilePath);

//删除已存在的目标文件

zipFile.delete();

}

cos = new CheckedOutputStream(new FileOutputStream(zipFile), new CRC32());

zos = new ZipOutputStream(cos);

//如果只是压缩一个文件,则需要截取该文件的父目录

String srcRootDir = srcPath;

if (srcFile.isFile())

{

int index = srcPath.lastIndexOf(File.separator);

if (index != -1)

{

srcRootDir = srcPath.substring(0, index);

}

}

//调用递归压缩方法进行目录或文件压缩

zip(srcRootDir, srcFile, zos);

zos.flush();

}

catch (Exception e) 

{

throw e;

}

finally 

{

try

{

if (zos != null)

{

zos.close();

}

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

/**

 * 解压缩zip包

 * @param zipFilePath zip文件的全路径

 * @param unzipFilePath 解压后的文件保存的路径

 * @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含

 */

@SuppressWarnings("unchecked")

public static void unzip(String zipFilePath, String unzipFilePath, boolean includeZipFileName) throws Exception

{

if (StringUtils.isEmpty(zipFilePath) || StringUtils.isEmpty(unzipFilePath))

{

throw new ParameterException(ICommonResultCode.PARAMETER_IS_NULL);

}

File zipFile = new File(zipFilePath);

//如果解压后的文件保存路径包含压缩文件的文件名,则追加该文件名到解压路径

if (includeZipFileName)

{

String fileName = zipFile.getName();

if (StringUtils.isNotEmpty(fileName))

{

fileName = fileName.substring(0, fileName.lastIndexOf("."));

}

unzipFilePath = unzipFilePath + File.separator + fileName;

}

//创建解压缩文件保存的路径

File unzipFileDir = new File(unzipFilePath);

if (!unzipFileDir.exists() || !unzipFileDir.isDirectory())

{

unzipFileDir.mkdirs();

}

//开始解压

ZipEntry entry = null;

String entryFilePath = null, entryDirPath = null;

File entryFile = null, entryDir = null;

int index = 0, count = 0, bufferSize = 1024;

byte[] buffer = new byte[bufferSize];

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

ZipFile zip = new ZipFile(zipFile);

EnumerationZipEntry entries = (EnumerationZipEntry)zip.entries();

//循环对压缩包里的每一个文件进行解压

while(entries.hasMoreElements())

{

entry = entries.nextElement();

//构建压缩包中一个文件解压后保存的文件全路径

entryFilePath = unzipFilePath + File.separator + entry.getName();

//构建解压后保存的文件夹路径

index = entryFilePath.lastIndexOf(File.separator);

if (index != -1)

{

entryDirPath = entryFilePath.substring(0, index);

}

else

{

entryDirPath = "";

}

entryDir = new File(entryDirPath);

//如果文件夹路径不存在,则创建文件夹

if (!entryDir.exists() || !entryDir.isDirectory())

{

entryDir.mkdirs();

}

//创建解压文件

entryFile = new File(entryFilePath);

if (entryFile.exists())

{

//检测文件是否允许删除,如果不允许删除,将会抛出SecurityException

SecurityManager securityManager = new SecurityManager();

securityManager.checkDelete(entryFilePath);

//删除已存在的目标文件

entryFile.delete();

}

//写入文件

bos = new BufferedOutputStream(new FileOutputStream(entryFile));

bis = new BufferedInputStream(zip.getInputStream(entry));

while ((count = bis.read(buffer, 0, bufferSize)) != -1)

{

bos.write(buffer, 0, count);

}

bos.flush();

bos.close();

}

}

public static void main(String[] args) 

{

String zipPath = "d:\ziptest\zipPath";

String dir = "d:\ziptest\rawfiles";

String zipFileName = "test.zip";

try

{

zip(dir, zipPath, zipFileName);

catch (Exception e)

{

e.printStackTrace();

}

String zipFilePath = "D:\ziptest\zipPath\test.zip";

String unzipFilePath = "D:\ziptest\zipPath";

try 

{

unzip(zipFilePath, unzipFilePath, true);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

java实现压缩视频文件,但是压缩后并解压,提示文件损坏,我该怎么修改代码?

(1)网络传输状况不好(如断线过多,开的线程过多,服务器人太多导致不能连接太多等)导致下载下来的文件损坏!

(2)站点提供的的RAR压缩包本来就是损坏的(这个本站可以保证,所上传的视频及软件等都经过好几遍测试,绝对没问题)。

(3)所使用的下载工具不够完善,比如有的下载工具多开了几个线程后,下载的收尾工作很慢,有些时候下载到99%时数据就不再传输了,一定要人工操作才能结束(先停止下载接着再开始)。笔者就碰到过好几次这样的情况。结果是文件下载下来以后解压缩到快结束时CRC出错。

解决方法:本站为防止这样的事情发生,在每个压缩包里又加了一个备份,防止因以上原因导致的下载后不能用,还得重新下载的问题,只要你下载下来的那个压缩包里的备份是好的那就能把压缩包里的文件恢复能用。

步骤一:双击打开需要解压修复的压缩包,选择:工具——修复压缩文件。

步骤二:出现下边图片的修复框,等待修复完成,关闭窗口及解压缩窗口就可以了。

步骤三:这时你会发现你需要解压的压缩包旁边多了一个压缩包,名称为:fixed.***(你下载的视频名称).rar ,这个压缩包就是修复后的解压缩包,如果修复成功,解压这个名称为:fixed.***(你下载的视频名称).rar 的压缩包就可以了。

如果修复不成功,你再修复几次看看,如果不行,只有再重新下载了

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。