java文件解壓和壓縮文件(java文件解壓和壓縮文件解壓區別)

本文目錄一覽:

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 的壓縮包就可以了。

如果修復不成功,你再修復幾次看看,如果不行,只有再重新下載了

原創文章,作者:ISIA,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/133911.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
ISIA的頭像ISIA
上一篇 2024-10-04 00:02
下一篇 2024-10-04 00:02

相關推薦

發表回復

登錄後才能評論