javazip解壓中文亂碼
『壹』 java 中文亂碼問題。崩潰了快。
你好,幫你看了一晚上,一直以為是程序轉碼的問題,看了半天的API和源碼,均沒有找到可以設置字元編碼的地方。上網一查,原來是jdk的問題,網上是這樣解釋的:
上網查了下,有兩種方法,一種修改jdk ZipInputStream及ZipOutputStream 的源文件,比較麻煩,不建議此項.第二種 就是拿來主義,因為 開源項目 Ant 里已經有現成的實現.把ant.jar 加入到 工程下的lib目錄即可.
在調試的過程中,發現了一些問題,幫你改正了下,你的程序打包後,裡面的層次關系錯亂了。只需要將其中的一句改為:
fileIn = new FileInputStream(fileName);
String entryName = fileName.getPath(); //這句,將fileName.getName()修改為getPath
// 生成的壓縮包存放在原目錄下
zipEntry = new ZipEntry(entryName) ;
這樣就正常了。
『貳』 java壓縮zip文件中文亂碼問題
我以前也遇到過這個問題,最後發現java自帶的zip壓縮沒辦法解決中文名亂碼的問題
你可以使用apache ant的zip類(package: org.apache.tools.zip)來解決這個問題。
『叄』 壓縮文件打開是亂碼怎麼辦
經常有網路朋友可能會遇到這樣的問題,當打開解壓後的軟體發現打開文件亂碼的情況。我們多數電腦中一般安裝都是WinRAR壓縮解壓軟體,在默認情況下,是可以雙擊打開RAR壓縮包里的文件的,一般遇到文件是亂碼的情況可能是壓縮文件本身的問題,也可能是我們解壓不當造成的問題,下面我為大家介紹以下相關解決辦法。
當我們雙擊打開壓縮包里的壓縮文件時,是操作系統系統調用相關的程序來打開壓縮包裡面的文件。
具體情況:打開壓縮包的文件,只能是先將壓縮包解壓,然後雙擊才能正常打開。如果是直接雙擊壓縮包里的文件,不知是什麼原因,雙擊壓縮包里的文檔、圖片都不能正常打開,打開後全部顯示是亂碼。為什麼雙擊壓縮包里的文件,會是亂碼呢?原因就在於誤設置了使用WinRAR的查看器來打開壓縮包里的文件。
要解決雙擊能正常打開壓縮包里的文件,而不是亂碼的問題,可以先啟動WinRAR軟體,然後單擊上面的菜單“選項”→“設置”→“查看器”選項卡,在“查看器類型”欄目里選中“關聯程序”選項即可解決雙擊打開壓縮文件為亂碼的問題。
如果以上方法都無法解決問題,那麼可能是壓縮文件本身壓縮的時候出現故障導致的,我們可以尋找下其他文件打開試試。
『肆』 java用apache的ZipEntry壓縮文件名為中文的word文件時,文件名亂碼
apache自帶的zip方法有缺陷,沒有做中文的判斷的,這個是它的一個已知bug。
解決辦法:用jdk的rt.jar裡面的方法實現就可以了。
可以參考下以下工具類:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
*
* @author gdb
*/
public class ZipUtilAll {
public static final int DEFAULT_BUFSIZE = 1024 * 16;
/**
* 解壓Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(File srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解壓Zip文件
*
* @param srcZipFile
* @param destDir
* @throws IOException
*/
public static void unZip(String srcZipFile, String destDir) throws IOException
{
ZipFile zipFile = new ZipFile(srcZipFile);
unZip(zipFile, destDir);
}
/**
* 解壓Zip文件
*
* @param zipFile
* @param destDir
* @throws IOException
*/
public static void unZip(ZipFile zipFile, String destDir) throws IOException
{
Enumeration<? extends ZipEntry> entryEnum = zipFile.entries();
ZipEntry entry = null;
while (entryEnum.hasMoreElements()) {
entry = entryEnum.nextElement();
File destFile = new File(destDir + entry.getName());
if (entry.isDirectory()) {
destFile.mkdirs();
}
else {
destFile.getParentFile().mkdirs();
InputStream eis = zipFile.getInputStream(entry);
System.out.println(eis.read());
write(eis, destFile);
}
}
}
/**
* 將輸入流中的數據寫到指定文件
*
* @param inputStream
* @param destFile
*/
public static void write(InputStream inputStream, File destFile) throws IOException
{
BufferedInputStream bufIs = null;
BufferedOutputStream bufOs = null;
try {
bufIs = new BufferedInputStream(inputStream);
bufOs = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] buf = new byte[DEFAULT_BUFSIZE];
int len = 0;
while ((len = bufIs.read(buf, 0, buf.length)) > 0) {
bufOs.write(buf, 0, len);
}
} catch (IOException ex) {
throw ex;
} finally {
close(bufOs, bufIs);
}
}
/**
* 安全關閉多個流
*
* @param streams
*/
public static void close(Closeable... streams)
{
try {
for (Closeable s : streams) {
if (s != null)
s.close();
}
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
/**
* @param args
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception
{
// unZip(new File(ZipDemo.class.getResource("D:/123/HKRT-B2B.zip").toURI()), "D:/123/");
unZip("D:/123/123.zip", "D:/123/");
// new File();
}
}
『伍』 java壓縮中文zip,系統winrar解壓改中文zip會出現亂碼
建議 使用 apache ant 工具包來做壓縮,能完美的解決中文亂碼問題
『陸』 如何解決java程序解壓含有中文名的zip壓縮包出現亂碼
上次利用java自動的java.util.zip.ZipEntry和�0�2java.util.zip.ZipFile來解壓zip文件,今天發現程序在讀取解壓文件時居然報了空指針異常,debug程序後發現時讀取不到文件,產生原先是zip壓縮文件中含有中文的名稱,讀取文件名為亂碼,
報找不到文件名,所以報了空指針,想到ant構建文件也有這個功能,換了apache的ant.jar居然解決了中文的問題。
備份下。
�0�2import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;/*** 讀取zip壓縮文件中文本的內容
* @author fish*/public class ReadZip {
public static void main(String args[]) {try {String fileName = "D:/workspace/java/src/ReadZip.zip";
//構造ZipFile
ZipFile zf = new ZipFile(new File(fileName));
//返回 ZIP file entries的枚舉.
Enumeration<? extends ZipEntry entries = zf.getEntries();
while (entries.hasMoreElements()) {
ZipEntry ze = entries.nextElement();
System.out.println("name:"+ze.getName());
long size = ze.getSize();
if (size 0) {
System.out.println("Length is " + size);
BufferedReader br = new BufferedReader(
new InputStreamReader(zf.getInputStream(ze)));
String line;
while ((line = br.readLine()) != null) {
『柒』 請問用java編寫一個壓縮程序,怎樣解決壓縮文件zip里的文件名亂碼問題!
沒做過壓縮程序,JAVA裡面的字元串使用的編碼為unicode,ZIP文件裡面用的應該是本地編碼(中文操作系統用的是GB2312)。
你可以嘗試著用類似這樣的語句:String str = ( otherStr.getBytes("GB2312") );
祝好運。
『捌』 用好壓軟體壓縮文件為zip格式,在用JAVA的zip4j包進行解壓,但是解壓後出現亂碼,怎麼事
按照你說的我沒有亂碼,把壓縮文件貼出來瞧瞧。