java中解壓縮亂碼解決方法
第一种使用ant实现的zip解压缩,其中解压的乱码注意使用
public void unZip(String unZipFileName,String outputPath) 其中
this.zipFile = new ZipFile(unZipFileName, "GB18030");是解决中文名乱码的关键。
import java.io.*; import org.apache.tools.zip.*; import java.util.Enumeration; /** *<p> * <b>功能:zip压缩、解压(支持中文文件名)</b> *<p> * 说明:使用Apache Ant提供的zip工具org.apache.tools.zip实现zip压缩和解压功能. * 解决了由于java.util.zip包不支持汉字的问题。 * * @author Winty * @modifier vernon.zheng */ public class AntZip { private ZipFile zipFile; private ZipOutputStream zipOut; // 压缩Zip private ZipEntry zipEntry; private static int bufSize; // size of bytes private byte[] buf; private int readedBytes; // 用于压缩中。要去除的绝对父路路径,目的是将绝对路径变成相对路径。 private String deleteAbsoluteParent; /** *构造方法。默认缓冲区大小为512字节。 */ public AntZip() { this(512); } /** *构造方法。 * * @param bufSize * 指定压缩或解压时的缓冲区大小 */ public AntZip(int bufSize) { this.bufSize = bufSize; this.buf = new byte[this.bufSize]; deleteAbsoluteParent = null; } /** *压缩文件夹内的所有文件和目录。 * * @param zipDirectory * 需要压缩的文件夹名 */ public void doZip(String zipDirectory) { File zipDir = new File(zipDirectory); doZip(new File[] { zipDir }, zipDir.getName()); } /** *压缩多个文件或目录。可以指定多个单独的文件或目录。而 <code>doZip(String zipDirectory)</code> * 则直接压缩整个文件夹。 * * @param files * 要压缩的文件或目录组成的<code>File</code>数组。 *@param zipFileName * 压缩后的zip文件名,如果后缀不是".zip", 自动添加后缀".zip"。 */ public void doZip(File[] files, String zipFileName) { // 未指定压缩文件名,默认为"ZipFile" if (zipFileName == null || zipFileName.equals("")) zipFileName = "ZipFile"; // 添加".zip"后缀 if (!zipFileName.endsWith(".zip")) zipFileName += ".zip"; try { this.zipOut = new ZipOutputStream(new BufferedOutputStream( new FileOutputStream(zipFileName))); compressFiles(files, this.zipOut, true); this.zipOut.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** *压缩文件和目录。由doZip()调用 * * @param files * 要压缩的文件 *@param zipOut * zip输出流 *@param isAbsolute * 是否是要去除的绝对路径的根路径。因为compressFiles() * 会递归地被调用,所以只用deleteAbsoluteParent不行。必须用isAbsolute来指明 * compressFiles()是第一次调用,而不是后续的递归调用。即如果要压缩的路径是 * E:\temp,那么第一次调用时,isAbsolute=true,则deleteAbsoluteParent会记录 * 要删除的路径就是E:\ ,当压缩子目录E:\temp\folder时,isAbsolute=false, * 再递归调用compressFiles()时,deleteAbsoluteParent仍然是E:\ 。从而保证了 * 将E:\temp及其子目录均正确地转化为相对目录。这样压缩才不会出错。不然绝对 路径E:\也会被写入到压缩文件中去。 */ private void compressFiles(File[] files, ZipOutputStream zipOut, boolean isAbsolute) throws IOException { for (File file : files) { if (file == null) continue; // 空的文件对象 // 删除绝对父路径 if (file.isAbsolute()) { if (isAbsolute) { deleteAbsoluteParent = file.getParentFile() .getAbsolutePath(); deleteAbsoluteParent = appendSeparator(deleteAbsoluteParent); } } else deleteAbsoluteParent = ""; if (file.isDirectory()) {// 是目录 compressFolder(file, zipOut); } else {// 是文件 compressFile(file, zipOut); } } } /** *压缩文件或空目录。由compressFiles()调用。 * * @param file * 需要压缩的文件 *@param zipOut * zip输出流 */ public void compressFile(File file, ZipOutputStream zipOut) throws IOException { String fileName = file.toString(); /* 去除绝对父路径。 */ if (file.isAbsolute()) fileName = fileName.substring(deleteAbsoluteParent.length()); if (fileName == null || fileName == "") return; /* * 因为是空目录,所以要在结尾加一个"/"。 不然就会被当作是空文件。 ZipEntry的isDirectory()方法中,目录以"/"结尾. * org.apache.tools.zip.ZipEntry : public boolean isDirectory() { return * getName().endsWith("/"); } */ if (file.isDirectory()) fileName = fileName + "/";// 此处不能用"\\" zipOut.putNextEntry(new ZipEntry(fileName)); // 如果是文件则需读;如果是空目录则无需读,直接转到zipOut.closeEntry()。 if (file.isFile()) { FileInputStream fileIn = new FileInputStream(file); while ((this.readedBytes = fileIn.read(this.buf)) > 0) { zipOut.write(this.buf, 0, this.readedBytes); } fileIn.close(); } zipOut.closeEntry(); } /** *递归完成目录文件读取。由compressFiles()调用。 * * @param dir * 需要处理的文件对象 *@param zipOut * zip输出流 */ private void compressFolder(File dir, ZipOutputStream zipOut) throws IOException { File[] files = dir.listFiles(); if (files.length == 0)// 如果目录为空,则单独压缩空目录。 compressFile(dir, zipOut); else // 如果目录不为空,则分别处理目录和文件. compressFiles(files, zipOut, false); } /** *解压指定zip文件。 * * @param unZipFileName * 需要解压的zip文件名 */ public void unZip(String unZipFileName) { FileOutputStream fileOut; File file; InputStream inputStream; try { this.zipFile = new ZipFile(unZipFileName); for (Enumeration entries = this.zipFile.getEntries(); entries .hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); file = new File(entry.getName()); if (entry.isDirectory()) {// 是目录,则创建之 file.mkdirs(); } else {// 是文件 // 如果指定文件的父目录不存在,则创建之. File parent = file.getParentFile(); if (parent != null && !parent.exists()) { parent.mkdirs(); } inputStream = zipFile.getInputStream(entry); fileOut = new FileOutputStream(file); while ((this.readedBytes = inputStream.read(this.buf)) > 0) { fileOut.write(this.buf, 0, this.readedBytes); } fileOut.close(); inputStream.close(); } } this.zipFile.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** *解压指定zip文件。其中"GB18030"解决中文乱码 * * @param unZipFileName * 需要解压的zip文件名 * @param outputPath * 输出路径 */ public void unZip(String unZipFileName,String outputPath) { FileOutputStream fileOut; File file; InputStream inputStream; try { this.zipFile = new ZipFile(unZipFileName, "GB18030"); for (Enumeration entries = this.zipFile.getEntries(); entries .hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); file = new File(outputPath+entry.getName()); if (entry.isDirectory()) {// 是目录,则创建之 file.mkdirs(); } else {// 是文件 // 如果指定文件的父目录不存在,则创建之. File parent = file.getParentFile(); if (parent != null && !parent.exists()) { parent.mkdirs(); } inputStream = zipFile.getInputStream(entry); fileOut = new FileOutputStream(file); while ((this.readedBytes = inputStream.read(this.buf)) > 0) { fileOut.write(this.buf, 0, this.readedBytes); } fileOut.close(); inputStream.close(); } } this.zipFile.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } /** *给文件路径或目录结尾添加File.separator * * @param fileName * 需要添加路径分割符的路径 *@return 如果路径已经有分割符,则原样返回,否则添加分割符后返回。 */ private String appendSeparator(String path) { if (!path.endsWith(File.separator)) path += File.separator; return path; } /** *解压指定zip文件。 * * @param unZipFile * 需要解压的zip文件对象 */ public void unZip(File unZipFile) { unZip(unZipFile.toString()); } /** *设置压缩或解压时缓冲区大小。 * * @param bufSize * 缓冲区大小 */ public void setBufSize(int bufSize) { this.bufSize = bufSize; } // 主函数,用于测试AntZip类 /* * public static void main(String[] args)throws Exception{ * if(args.length>=2){ AntZip zip = new AntZip(); * * if(args[0].equals("-zip")){ //将后续参数全部转化为File对象 File[] files = new File[ * args.length - 1]; for(int i = 0;i < args.length - 1; i++){ files = new * File(args[i + 1]); } * * //将第一个文件名作为zip文件名 zip.doZip(files , files[0].getName()); * * return ; } else if(args[0].equals("-unzip")){ zip.unZip(args[1]); return * ; } } * * System.out.println("Usage:"); * System.out.println("压缩:java AntZip -zip [directoryName | fileName]... "); * System.out.println("解压:java AntZip -unzip fileName.zip"); } */ }
第二种 从修改ZipInputStream及ZipOutputStream对於档名的编码方式来着手了。
我们可以从jdk的src.zip取得ZipInputStream及ZipOutputStream的原始码来加以修改:
一、ZipOutputStream.java
1.从jdk的src.zip取得ZipOutputStream.java原始码,另存新档存到c:/java/util/zip这个资料夹里,档名改为CZipOutputStream.java。
2.开始修改原始码,将class名称改为CZipOutputStream
3.建构式也必须更改为CZipOutputStream
4.新增member,这个member记录编码方式
private String encoding="UTF-8";
5.再新增一个建构式(这个建构式可以让这个class在new的时候,设定档名的编码)
public CZipOutputStream(OutputStream out,String encoding) { super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true)); usesDefaultDeflater = true; this.encoding=encoding; }
推荐:java基础教程
以上是java中解壓縮亂碼解決方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

膠囊是一種三維幾何圖形,由一個圓柱體和兩端各一個半球體組成。膠囊的體積可以通過將圓柱體的體積和兩端半球體的體積相加來計算。本教程將討論如何使用不同的方法在Java中計算給定膠囊的體積。 膠囊體積公式 膠囊體積的公式如下: 膠囊體積 = 圓柱體體積 兩個半球體體積 其中, r: 半球體的半徑。 h: 圓柱體的高度(不包括半球體)。 例子 1 輸入 半徑 = 5 單位 高度 = 10 單位 輸出 體積 = 1570.8 立方單位 解釋 使用公式計算體積: 體積 = π × r2 × h (4

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。
