commons compress使用+ziji
此文介紹apache的壓縮包,好處是可以自己設(shè)置ZipFile文件名的編碼.
而jdk自帶的ZipFIle只支持utf-8,導(dǎo)致壓縮后中文出現(xiàn)亂碼.
?
*下面給出個(gè)zip壓縮解壓的示例
---zip壓縮
private void doZip() throws ArchiveException, IOException{ZipArchiveOutputStream zos =(ZipArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("zip", new FileOutputStream(path)); //or new ZipArchiveOutputStream(new FileOutputStream(path)) zos.setEncoding(encoding);String rootPath =FileUtil.getRootPath(files); //獲取要壓縮文件根路徑ZipArchiveEntry ze;for(File f:files){if(!f.exists())continue;ze =new ZipArchiveEntry(getEntryName(f,rootPath)); //獲取每個(gè)文件相對(duì)路徑,作為在ZIP中路徑zos.putArchiveEntry(ze);//folderif(ze.isDirectory()){zos.closeArchiveEntry();continue;}//fileFileInputStream fis =new FileInputStream(f);IOUtils.copy(fis, zos, BUF);fis.close();zos.closeArchiveEntry();}zos.close(); }private String getEntryName(File f,String rootPath){String entryName;String fPath =f.getAbsolutePath();if(fPath.indexOf(rootPath)!=-1)entryName =fPath.substring(rootPath.length()+1);elseentryName =f.getName();if(f.isDirectory())entryName +="/"; //"/"后綴表示entry是文件夾return entryName; }?
---zip解壓
public void doZip() throws IOException, ArchiveException{ZipFile file =new ZipFile(sPath,encoding);Enumeration<ZipArchiveEntry> en =file.getEntries();ZipArchiveEntry ze;while(en.hasMoreElements()){ze =en.nextElement();File f =new File(tPath,ze.getName());//創(chuàng)建完整路徑if(ze.isDirectory()){ f.mkdirs();continue;}elsef.getParentFile().mkdirs(); InputStream is =file.getInputStream(ze);OutputStream os =new FileOutputStream(f);IOUtils.copy(is, os, BUF);is.close();os.close();}file.close(); }//這是更一般的解壓處理 public void doTar() throws IOException{TarArchiveInputStream tis =new TarArchiveInputStream(new FileInputStream(sPath));TarArchiveEntry te =null;while((te=tis.getNextTarEntry())!=null){File target =new File(tPath,te.getName());if(te.isDirectory()){target.mkdirs();continue;}elsetarget.getParentFile().mkdirs();FileOutputStream fos =new FileOutputStream(target); //將當(dāng)前entry寫(xiě)入文件byte[] buf =new byte[BUF];int len;while((len=tis.read(buf))!=-1){fos.write(buf, 0, len);}fos.close();}tis.close(); }?
*tar包
---默認(rèn)tar中entry的長(zhǎng)度限制是TarConstants.NAMELEN = 100 [0x64]
原因有待研究?
這個(gè)長(zhǎng)度應(yīng)當(dāng)=操作系統(tǒng)支持的最小長(zhǎng)度.
---長(zhǎng)文件名處理setLongFileMode(int longFileMode)
參數(shù)
/** Fail if a long file name is required in the archive. */
public static final int LONGFILE_ERROR = 0;
/** Long paths will be truncated in the archive. */
public static final int LONGFILE_TRUNCATE = 1;
/** GNU tar extensions are used to store long file names in the archive. */
public static final int LONGFILE_GNU = 2;
在putArchiveEntry()中根據(jù)上面的設(shè)置處理文件名.
使用長(zhǎng)文件名的設(shè)置缺點(diǎn)是在其他非gnu操作系統(tǒng)上可能無(wú)法解壓
"If you choose the GNU tar option, the archive can not be extracted using many other tar implementations like the ones of OpenBSD, Solaris or MacOS X"
---注意
"The tar package does not support the full POSIX tar standard nor more modern GNU extension of said standard. It cannot deal with entries larger than 2 GByte either. "
?
*Jar包
“Note that ArchiveStreamFactory doesn't distinguish ZIP archives from JAR archives, so if you use the one-argument createArchiveInputStream? method on a JAR archive, it will still return the more generic ZipArchiveInputStream.
The JarArchiveEntry class contains fields for certificates and attributes that are planned to be supported in the future but are not supported as of Compress 1.0.”
從源碼可知JarArchive*Stream和JarArchiveEntry繼承自對(duì)應(yīng)的Zip,從說(shuō)明可知,目前兩者功能是一致的
package com.me;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;
public class UnZip {
?
?private static final String path = "D://WorkSpace//111.zip";
?
?private static final String tpath = "D://WorkSpace//";
?
?public static void main(String[] args) throws IOException
?{
??//File file = new File(path);
??ZipFile zipFile = new ZipFile(path);
??Enumeration<ZipArchiveEntry> en = zipFile.getEntries();
??ZipArchiveEntry ze;
??while(en.hasMoreElements())
??{
???ze = en.nextElement();
???File file = new File(tpath,ze.getName());
???//創(chuàng)建完整路徑
???if(ze.isDirectory())
???{
????file.mkdirs();
????continue;
???}else{
????file.getParentFile().mkdirs();
???}
???InputStream is =zipFile.getInputStream(ze);
???OutputStream os =new FileOutputStream(file);
???IOUtils.copy(is, os);
???is.close();
???os.close();
??}
??zipFile.close();??
?}
}
總結(jié)
以上是生活随笔為你收集整理的commons compress使用+ziji的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python训练营 朋友圈点赞收费吗_千
- 下一篇: 使用docker搭建xss挑战之旅环境,