1. 程式人生 > >史上最簡單解決ZipOutputStream壓縮檔案中文名亂碼問題

史上最簡單解決ZipOutputStream壓縮檔案中文名亂碼問題

用關鍵字“ZipOutputStream 壓縮 中文亂碼問題”一大把,無非是兩種方法:

第一種就是改JDK原始碼, 把ZipOutputStream拷貝出來,修改下編碼

第二種就是用apache-ant

這兩種方法詳見http://blog.csdn.net/zhanghw0917/article/details/5628039

這裡介紹一種最簡單的辦法,不用改原始碼,也不用換apache-ant,上面這兩種辦法出現中文亂碼的問題都是基於JDK6,但是在JDK7中已經解決了,只要把JDK版本升到7就可以。

從兩個版本的原始碼比較就知道了:

JDK6u21的ZipOutputStream.java的原始碼片段:

	/**
	 * Creates a new ZIP output stream.
	 * 
	 * @param out
	 * the actual output stream
	 */
	public ZipOutputStream(OutputStream out) {
		super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
		usesDefaultDeflater = true;
	}
	
	/**
	 * Sets the ZIP file comment.
	 * 
	 * @param comment
	 * the comment string
	 * @exception IllegalArgumentException
	 * if the length of the specified ZIP file comment is greater
	 * than 0xFFFF bytes
	 */
	public void setComment(String comment) {
		if (comment != null && comment.length() > 0xffff / 3
				&& getUTF8Length(comment) > 0xffff) {
			throw new IllegalArgumentException("ZIP file comment too long.");
		}
		this.comment = comment;
	}


JDK7u65的ZipOutputStream.java的原始碼片段:
/**
     * Creates a new ZIP output stream.
     *
     * <p>The UTF-8 {@link java.nio.charset.Charset charset} is used
     * to encode the entry names and comments.
     *
     * @param out the actual output stream
     */
    public ZipOutputStream(OutputStream out) {
        this(out, StandardCharsets.UTF_8);
    }

    /**
     * Creates a new ZIP output stream.
     *
     * @param out the actual output stream
     *
     * @param charset the {@linkplain java.nio.charset.Charset charset}
     *                to be used to encode the entry names and comments
     *
     * @since 1.7
     */
    public ZipOutputStream(OutputStream out, Charset charset) {
        super(out, new Deflater(Deflater.DEFAULT_COMPRESSION, true));
        if (charset == null)
            throw new NullPointerException("charset is null");
        this.zc = ZipCoder.get(charset);
        usesDefaultDeflater = true;
    }

    /**
     * Sets the ZIP file comment.
     * @param comment the comment string
     * @exception IllegalArgumentException if the length of the specified
     *            ZIP file comment is greater than 0xFFFF bytes
     */
    public void setComment(String comment) {
        if (comment != null) {
            this.comment = zc.getBytes(comment);
            if (this.comment.length > 0xffff)
                throw new IllegalArgumentException("ZIP file comment too long.");
        }
    }

從JDK6版本中可以看出,只有一個帶OutputStream引數的構造方法,並且沒有傳入字符集,而JDK7版本中的有兩個構造方法,一個是帶OutputStream和Charset的構造方法,另外一個就是過載,只有一個OutputStream,預設字符集UTF-8。


相關推薦

簡單解決ZipOutputStream壓縮檔案中文名亂碼問題

用關鍵字“ZipOutputStream 壓縮 中文亂碼問題”一大把,無非是兩種方法: 第一種就是改JDK原始碼, 把ZipOutputStream拷貝出來,修改下編碼 第二種就是用apache-ant 這兩種方法詳見http://blog.csdn.net/zhanghw

查找(一)簡單清晰的紅黑樹解說

ont 演示 detail align article 向上 節點 動態插入 列表 查找(一) 我們使用符號表這個詞來描寫敘述一張抽象的表格。我們會將信息(值)存儲在當中,然後依照指定的鍵來搜索並獲取這些信息。鍵和值的詳細意義取決於不同的應用。 符號表中可能會保

掛載文件系統出現"kernel panic..." 解決方案

某個文件 table sha mount nic mic 2.6 完成 又是   問:掛載自己制作的文件系統卡在這裏:    NET: Registered protocol family 1    NET: Registered protocol family 17   

Intellij idea簡單的教程之Linux下安裝與破解Intellij idea2017

成功 zxvf java 新建 pre form 旗艦版 lan intel 一、前言 這一節我們介紹在Linux下如何安裝與破解Intellij idea2017。現在有很多公司開發環境都是Linux,所以掌握在Linux環境下使用Idea辦公也是咱們必須得掌握的技能。

簡單的ftp下載服務器

spa 最簡 pre 服務器 pat ror tin total true 服務端: 1 # encoding:utf-8 2 # Author:"richie" 3 # Date:8/23/2017 4 5 from socket import * 6 i

簡單的 SpringCloud 教程 | 終章

conf con 知識 實例 feign 地址 聚合 com red 版權聲明:本文為博主原創文章,歡迎轉載,轉載請註明作者、原文超鏈接 ,博主地址:http://blog.csdn.net/forezp。 目錄(?)[+] 轉載請標明出處: http

簡單的springboot國際化多語言切換實現方案

messages conf main del span 語言 rop target 每天 每天學習一點點 編程PDF電子書、視頻教程免費下載:http://www.shitanlife.com/code 前提: 在resources目錄下建立 messages_en

這恐怕是簡單傳 AAR 教程

Gradle 依賴管理 Android Studio 類庫 在做項目的時候,抽出來一些工具方法,然後做其他項目的時候需要使用,只是後就有將這些工具方法抽出來單獨形成一個庫的需求了。在 Android Studio 裏就是 AAR。因為我是個人開發,想到方便的方法就是把 AAR 上傳到本地 ma

簡單的 SpringCloud 教程 | 第十四篇: 服務註冊(consul)

配置 資料 源碼下載 擴展性 local sta tar value mark 這篇文章主要介紹 spring cloud consul 組件,它是一個提供服務發現和配置的工具。consul具有分布式、高可用、高擴展性。 consul 具有以下性質: 服務發現:cons

簡單的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)

prope shu 由於 ext master strip div 文件配置 rap 在上一篇文章講述zuul的時候,已經提到過,使用配置服務來保存各個服務的配置文件。它就是Spring Cloud Config。 在分布式系統中,由於服務數量巨多,為了方便服務配置文件統

簡單的SpringCloud教程 | 第五篇: 路由網關(zuul)(Finchley版本)

開頭 proxy 打開 系統 blog 註冊 hub 需要 ews 在微服務架構中,需要幾個基礎的服務治理組件,包括服務註冊與發現、服務消費、負載均衡、斷路器、智能路由、配置管理等,由這幾個基礎組件相互協作,共同組建了一個簡單的微服務系統。一個簡答的微服務系統如下圖: 註

簡單的SpringCloud教程 | 第五篇: 路由網關(zuul)

under 中心 sig ribbon 請求 安全 src htm span 在微服務架構中,需要幾個基礎的服務治理組件,包括服務註冊與發現、服務消費、負載均衡、斷路器、智能路由、配置管理等,由這幾個基礎組件相互協作,共同組建了一個簡單的微服務系統。一個簡答的微服務系統如

簡單的SpringCloud教程 | 第二篇: 服務消費者(rest+ribbon)

image tree 開啟 then rom cat learn 替代 官網 最新Finchley版本:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/或者http://blog.csdn.n

簡單的 SpringCloud 教程 | 第一篇: 服務的註冊與發現(Eureka)

add 過程 sdn 需要 2.3 boot one ini tail 最新Finchley版本請訪問:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/或者http://blog.csdn.n

簡單的SpringCloud教程 | 第十三篇: 斷路器聚合監控(Hystrix Turbine)

打開 jsb cli fill alt 數據 需要 eap south 當我們有很多個服務的時候,這就需要聚合所以服務的Hystrix Dashboard的數據了。這就需要用到Spring Cloud的另一個組件了,即Hystrix Turbine。 看單個的Hystri

簡單的SpringCloud教程 | 第十二篇: 斷路器監控(Hystrix Dashboard)

詳細 pre 良好的 依次 alt ews 需要 ext 數據監控 最新Finchley版本,請訪問:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f12-dash/或者http://blog.csdn.net

簡單的SpringCloud教程 | 第四篇:斷路器(Hystrix)

技術分享 熔斷器 enable layer get local nsh 12c host 在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以相互調用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調用。為了保證

簡單的SpringCloud教程 | 第十一篇: docker部署spring cloud項目

大數 imageview 建議 chapter 環境 多次 pan mas 存儲 Docker是一個開源的引擎,可以輕松的為任何應用創建一個輕量級的、可移植的、自給自足的容器。開發者在筆記本上編譯測試通過的容器可以批量地在生產環境中部署,包括VMs(虛擬機)、bare m

簡單的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)

tex down 代碼 多少 erl ogr 管理 變量 實時 最新Finchley版本:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f6-config/或者http://blog.csdn.net/fore

簡單的SpringCloud教程 | 第十篇: 高可用的服務註冊中心

pad 配置 設置ip systems 高可用性 多個 could hostname 打開 最新Finchley版本請訪問:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f10-eureka/或者http://