java中zip与gzip实现文件压缩,解压
2017-03-22 14:20
691 查看
GZIP对文件进行压缩解压,代码如下:
ZIP对一个文件夹中的文件进行压缩和解压,代码如下:
小结:压缩文本文件一般通过字符操作压缩,压缩过程中指定编码的格式,解压过程中也通过指定解压的格式,最后获得准确解压文件,格式指定错误,将得到乱码的文件。
package cn.com.normal.io; import java.io.*; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * GZIP压缩和解压 */ public class GZIP { /** * gzip文件压缩 * @param inputFile * @param targetName */ public void compressFile(File inputFile,String targetName) throws IOException { System.out.println("程序开始..."); //字符输入流,以utf-8格式读入 BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(inputFile),"utf-8")); //字符输出流 BufferedOutputStream out=new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(targetName))); int n; while ((n=in.read())!=-1){ out.write(String.valueOf((char) n).getBytes("utf-8")); } in.close(); out.close(); System.out.println("压缩完毕..."); } /** * 解压文件 * @param inputFile * @param targetName * @throws IOException */ public void decompressFile(File inputFile,String targetName) throws IOException { BufferedReader reader=new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(inputFile)),"utf-8")); String s; FileOutputStream fos=new FileOutputStream(targetName); while ((s=reader.readLine())!=null){ fos.write(s.getBytes()); } reader.close(); fos.close(); System.out.println("解压完毕..."); } }
ZIP对一个文件夹中的文件进行压缩和解压,代码如下:
package cn.com.normal.io; import java.io.*; import java.nio.charset.Charset; import java.util.zip.GZIPInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; /** * ZIP压缩方式 */ public class ZIP { private void _zip(String zipEntry,File file,ZipOutputStream out,BufferedOutputStream os) throws IOException { if(file.isDirectory()){ File[] files=file.listFiles(); if(file.length()==0){ out.putNextEntry(new ZipEntry(zipEntry+File.separator)); }else{ for(File f:files){ _zip(zipEntry+File.separator+f.getName(),f,out,os); } } }else{ out.putNextEntry(new ZipEntry(zipEntry)); FileInputStream in=new FileInputStream(file); BufferedInputStream bin=new BufferedInputStream(in); int n; while ((n=bin.read())!=-1){ os.write(n); } bin.close(); in.close(); } } public void zip(String output,File input) throws IOException { FileOutputStream fos=new FileOutputStream(output); ZipOutputStream out=new ZipOutputStream(fos); BufferedOutputStream os=new BufferedOutputStream(out); _zip(input.getName(),input,out,os); os.close(); out.close(); } public void unzip(String directory,String inputFile) throws IOException { ZipInputStream zin=new ZipInputStream(new FileInputStream(inputFile)); BufferedInputStream bin=new BufferedInputStream(zin); ZipEntry entry=null; File file=null; while ((entry=zin.getNextEntry())!=null&&!entry.isDirectory()){ file=new File(directory,entry.getName()); if(!file.exists()){ (new File(file.getParent())).mkdirs(); } FileOutputStream out=new FileOutputStream(file); BufferedOutputStream bout=new BufferedOutputStream(out); int n; while ((n=bin.read())!=-1){ bout.write(n); } out.close(); bout.close(); } bin.close(); zin.close(); } }
小结:压缩文本文件一般通过字符操作压缩,压缩过程中指定编码的格式,解压过程中也通过指定解压的格式,最后获得准确解压文件,格式指定错误,将得到乱码的文件。
相关文章推荐
- Java实现文件压缩与解压[zip格式,gzip格式]
- Java实现文件压缩与解压的示例[zip格式,gzip格式]
- java实现zip文件压缩,解压
- Java实现文件压缩与解压[zip格式,gzip格式]
- java实现将ZIP压缩文件解压的工具类
- Java实现文件压缩与解压[zip格式,gzip格式]
- 用java代码实现文件的zip压缩与解压
- Java实现文件压缩与解压[zip格式,gzip格式]
- Java实现文件压缩与解压[zip格式,gzip格式]
- java实现将ZIP压缩文件解压的工具类
- Java实现文件压缩与解压[zip格式,gzip格式]
- Java实现文件压缩与解压[zip格式,gzip格式]
- java 解压zip压缩文件
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- JAVA实现实用的ZIP压缩与解压
- 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径
- JAVA实现实用的ZIP压缩与解压
- 在服务器上的用ZIP压缩或用GZIP压缩的文件,在客户端解压来读
- Android 利用Java实现压缩与解压缩(zip、gzip)支持中文路径