通過java實現解壓zip,rar的程式碼
package com.zuidaima.main; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URL; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import com.github.junrar.Archive; import com.github.junrar.exception.RarException; import com.github.junrar.rarfile.FileHeader; /** **/ public class Main { public void extractZipFiles() { String root ="c:/javazip/"; URL url = Main.class.getResource("/aa.zip"); String filename = url.getFile(); try { // destination folder to extract the contents byte[] buf = new byte[1024]; ZipInputStream zipinputstream = null; ZipEntry zipentry; zipinputstream = new ZipInputStream(new FileInputStream(filename)); zipentry = zipinputstream.getNextEntry(); while (zipentry != null) { // for each entry to be extracted String entryName = zipentry.getName(); System.out.println(entryName); int n; FileOutputStream fileoutputstream; File newFile = new File(entryName); String directory = newFile.getParent(); // to creating the parent directories if (directory == null) { if (newFile.isDirectory()) { break; } } else { new File(root + directory).mkdirs(); } if (!zipentry.isDirectory()) { System.out.println("File to be extracted....."+ entryName); fileoutputstream = new FileOutputStream(root + entryName); while ((n = zipinputstream.read(buf, 0, 1024)) > -1) { fileoutputstream.write(buf, 0, n); } fileoutputstream.close(); } zipinputstream.closeEntry(); zipentry = zipinputstream.getNextEntry(); } zipinputstream.close(); } catch (Exception e) { e.printStackTrace(); } } public void extractRarFiles() { URL url = Main.class.getResource("/bb.rar"); String filename = url.getFile(); File archive = new File(filename); File destination = new File("c:/javazip/"); Archive arch = null; try { arch = new Archive(archive); } catch (RarException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (arch != null) { if (arch.isEncrypted()) { System.out.println("archive is encrypted cannot extreact"); return; } FileHeader fh = null; while (true) { fh = arch.nextFileHeader(); if (fh == null) { break; } if (fh.isEncrypted()) { System.out.println("file is encrypted cannot extract:" + fh.getFileNameString()); continue; } System.out.println("extracting:"+ fh.getFileNameString()); try { if (fh.isDirectory()) { createDirectory(fh, destination); } else { File f = createFile(fh, destination); OutputStream stream = new FileOutputStream(f); arch.extractFile(fh, stream); stream.close(); } } catch (Exception e) { e.printStackTrace(); } } } } private File createFile(FileHeader fh, File destination) { File f = null; String name = null; if (fh.isFileHeader() && fh.isUnicode()) { name = fh.getFileNameW(); } else { name = fh.getFileNameString(); } f = new File(destination, name); if (!f.exists()) { try { f = makeFile(destination, name); } catch (IOException e) { e.printStackTrace(); } } return f; } private File makeFile(File destination, String name) throws IOException { String[] dirs = name.split("\"); if (dirs == null) { return null; } String path =""; int size = dirs.length; if (size == 1) { return new File(destination, name); } else if (size > 1) { for (int i = 0; i < dirs.length - 1; i++) { path = path + File.separator + dirs[i]; new File(destination, path).mkdir(); } path = path + File.separator + dirs[dirs.length - 1]; File f = new File(destination, path); f.createNewFile(); return f; } else { return null; } } private void createDirectory(FileHeader fh, File destination) { File f = null; if (fh.isDirectory() && fh.isUnicode()) { f = new File(destination, fh.getFileNameW()); if (!f.exists()) { makeDirectory(destination, fh.getFileNameW()); } } else if (fh.isDirectory() && !fh.isUnicode()) { f = new File(destination, fh.getFileNameString()); if (!f.exists()) { makeDirectory(destination, fh.getFileNameString()); } } } private void makeDirectory(File destination, String fileName) { String[] dirs = fileName.split("\"); if (dirs == null) { return; } String path =""; for (String dir : dirs) { path = path + File.separator + dir; new File(destination, path).mkdir(); } } public static void main(String[] args) { Main main = new Main(); // extract zip main.extractZipFiles(); // extract rar main.extractRarFiles(); } }
相關推薦
通過java實現解壓zip,rar的程式碼
package com.zuidaima.main; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.i
【成功解決】ubuntu 12.04 解壓 zip 7z rar jar 檔案,檔名中文亂碼的問題
.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName(注:tar是打包,不是壓縮!) ---------------------------------------------.gz 解壓1:gunzip FileName.gz 解壓2:gz
java解壓zip、rar(多級檔案)
/** * 解壓上傳檔案 * @param importZipFilePath * 上傳檔案地址 * @param importFilePath * 解壓檔案存放地址 * @param cancel
java 壓縮/解壓 zip 多個檔案和資料夾
參考了幾篇文章,基本都是壓縮單個檔案或者一個資料夾,不能混合壓縮。 找了一個不需要額外jar包的程式碼上改的。(參考文章) 不需要額外jar包。 壓縮方法: import java.io.File; import java.io.FileInputStr
Linux(Centos6.5)下如何解壓.zip和.rar檔案
Windows下常見的壓縮檔案型別是.zip和.rar。在Linux下如何壓縮和解壓縮呢? 1、zip linux下提供了zip和unzip命令。zip是壓縮,unzip是解壓縮。舉例如下: #zip myFile.zip *jpg 將所有jpg檔案壓縮成一個壓縮包。 #u
Linux 下壓縮與解壓.zip和.rar
對於Window下的常見壓縮檔案.zip和.rar,Linux也有相應的方法來解壓它們:1)對於.zip linux下提供了zip和unzip程式,zip是壓縮程式,unzip是解壓程式。它們的引數選項很多,可用命令zip -help和unzip -help檢視,這裡只做簡單介紹,舉例說明一下其用法: # z
Ubuntu下如何解壓.zip和.rar格式的壓縮檔案
linux下提供了zip和unzip程式,zip是壓縮程式,unzip是解壓程式。它們的引數選項很多,這裡只做簡單介紹,舉例說明一下其用法:# zip all.zip *.jpg 這條命令是將所有.jpg的檔案壓縮成一個zip包# unzip all.zip 這條命令是將all.zip中的所有檔案解壓出來
java實現上傳zip解壓及判斷壓縮包資料夾功能
直接上Service,通過程式碼看思路貫穿整個功能,很多工具類可以複用,檔案路徑可以去看我部落格裡的(使用ResourceBundle國際化資原始檔讀取properties詳解) 這篇製作方法url:https://blog.csdn.net/qq_17025903/arti
scala和java解壓zip的檔案,並上傳到hdfs伺服器
之前一直使用的事java開發的,後來學習hadoop,spark以後,需要做一個解壓檔案到伺服器的例子,由於學習scala時間不長,故先用java程式碼寫出來,在改成scala的程式碼,過程中出現了很多問題,我將會在最後說明,請大家注意不要少了包。 環境宣告:windows
Java 解壓zip壓縮包
ice gin inpu ret zipentry .get get next exce 因為最近項目需要批量上傳文件,而這裏的批量就是將文件壓縮在了一個zip包裏,然後讀取文件進行解析文件裏的內容。 因此需要先對上傳的zip包進行解壓。以下直接提供代碼供參考: 1.第一個
java解壓zip文件至指定文件夾
ava tel exceptio 失敗 ppr mark roc types EDA 前面,筆者講到。如何把文件打包為zip包,那麽反過來怎麽把zip文件包解壓為正常文件呢?把zip包解壓為正常文件包,要比把文件打包為zip簡單一點。因為存在多級文件的壓縮,卻不存在多級文件
Java 壓縮解壓通用工具包 ZIP TAR GZ
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutp
mysql 5.7.21 解壓版,通過歷史data目錄恢復資料
情況說明: 資料庫非正常啟動關閉導致再次啟動服務的時候,提示一直“正在啟動”,無奈只好重新安裝該解壓版程式。 安裝成功並生成data檔案目錄,設定好使用者密碼之類的,目錄效果如下: 下面進行資料恢復,很簡單。 1.找到之前(我之前一直啟動不成功路徑F:\mysq
通過Java實現HMAC,將字串雜湊成為全部由字母組成的密文串
以下Java程式碼可以將任何字串通過HMAC雜湊,並輸出成由大寫的A到P組成的密文字串。 public class HMAC { private final static String KEY_MAC = "HMACMD5"; /** * 全域性陣列 *
Java 複製zip檔案到指定目錄並解壓zip檔案
過程中使用apache的ant jar包 apache-ant-1.8.2.jar 示例程式碼如下: import org.apache.log4j.Logger; import java.io.File; import java.io.IOException;
上傳檔案,解壓zip
前臺使用layui官方上傳檔案例子: <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title&
java 解壓zip中文檔案 出現錯誤
java 解壓zip中文檔案 出現錯誤 java.lang.IllegalArgumentException: MALFORMED 解決這個錯誤需要 jdk 1.7及以上版本支援 並在建立ZipFile 時指定中文字符集gbk ZipFile zipFile = new
Java無需解壓直接讀取Zip檔案和檔案內容
package test; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.
java通過cxf實現webservice服務端,客戶端接收不到響應資訊處理方法
最近在做專案時與客戶進行webservice聯調,響應成功後返回1,服務端正常返回,客戶端表示沒有收到,針對其中的問題分析和處理方法。 1.判斷是否是服務寫的有問題,寫了個測試方式試了下,發現服務端返回沒有問題 String xmlStr = "<?xml vers
Java合併解壓多省話單gz大檔案,程式碼涉及解壓、合併、刪除、複製邏輯
專案要求是能定時解壓gz檔案,並按照省將檔案成一個大檔案,程式碼實現功能:按照gz壓縮包的路徑格式解壓檔案,最後將檔案合併成按照省份名稱排放的大檔案,專案用log4j記錄日誌,用Java定時器實現定時解壓合併,並且刪除解壓出來的小檔案,同時也刪除解壓目錄下的壓縮包,並將壓縮