Java 实现压缩文件
2012-10-17 10:27
120 查看
Java comes with “java.util.zip” library to implement the data compression in ZIp format. The overall concept is quite straightforward.
·Read file with “FileInputStream”
·Add the file name to “ZipEntry” and output it to “ZipOutputStream“
Simple ZIP example
Read a file “C:\\user.txt” and compress it into a zip file – “C:\\user.zip“.
Advance ZIP example – Recursively
Read all files from folder “C:\\Logs” and compress it into a zip file – “C:\\Logs.zip“. It will recursively zip a directory as well.
·Read file with “FileInputStream”
·Add the file name to “ZipEntry” and output it to “ZipOutputStream“
Simple ZIP example
Read a file “C:\\user.txt” and compress it into a zip file – “C:\\user.zip“.
package org.hello.zip; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class JavaZip { public static void main(String[] args) { byte[] buffer = new byte[1024]; try { FileOutputStream fos = new FileOutputStream("C:\\user.zip"); ZipOutputStream zos = new ZipOutputStream(fos); ZipEntry ze = new ZipEntry("user.txt"); zos.putNextEntry(ze); FileInputStream in = new FileInputStream("C:\\user.txt"); /* * int java.io.FileInputStream.read(byte[] b) throws IOException * Reads up to b.length bytes of data from this input stream into an * array of bytes. This method blocks until some input is available. * Overrides: read(...) in InputStream Parameters: b the buffer into * which the data is read. Returns: the total number of bytes read * into the buffer, or -1 if there is no more data because the end * of the file has been reached. */ int len; while ((len = in.read(buffer)) > 0) { zos.write(buffer, 0, len); } in.close(); zos.closeEntry(); zos.close(); System.out.println("Compress Completed!"); } catch (IOException e) { e.printStackTrace(); } } }
Advance ZIP example – Recursively
Read all files from folder “C:\\Logs” and compress it into a zip file – “C:\\Logs.zip“. It will recursively zip a directory as well.
package org.hello.zip; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class AppZip { List<String> fileList; private static final String OUTPUT_ZIP_FILE = "C:\\Logs.zip"; private static final String SOURCE_FOLDER = "C:\\Logs"; AppZip(){ fileList =new ArrayList<String>(); } public static void main(String[] args){ AppZip appzip = new AppZip(); appzip.generateFileList(new File(SOURCE_FOLDER)); appzip.zipIt(OUTPUT_ZIP_FILE); } private void zipIt(String zipFile) { byte[] buffer = new byte[1024]; try{ FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); System.out.println("Output to Zip: "+ zipFile); for(String filename :this.fileList){ System.out.println("File added: "+filename); ZipEntry ze = new ZipEntry(filename); zos.putNextEntry(ze); FileInputStream in = new FileInputStream(SOURCE_FOLDER + File.separator + filename); int len; while((len = in.read(buffer)) > 0){ zos.write(buffer,0,len); } in.close(); } zos.closeEntry(); zos.close(); System.out.println("Compress Completed."); }catch(IOException e){ e.printStackTrace(); } } /** * Traverse a directory and get all files, * and add the file into fileList * @param node file or directory */ private void generateFileList(File node) { //add file only if(node.isFile()){ fileList.add(generateZipEntry(node.getAbsoluteFile().toString())); } if(node.isDirectory()){ String[] subNote = node.list(); for(String filename : subNote){ generateFileList(new File(node, filename)); } } } /** * Format the file path for zip * @param file file path * @return Formatted file path */ private String generateZipEntry(String filename) { return filename.substring(SOURCE_FOLDER.length()+1, filename.length()); } }
相关文章推荐
- 用Java代码实现文件的压缩
- java 文件和文件夹的压缩实现
- Java 编程之实现文件或目录的压缩.
- java实现zip压缩文件
- java实现文件压缩并加密-基于zip4j
- JAVA实现压缩文件的方法(使用java.util.zip.*)
- java实现zip压缩文件/文件夹
- java实现将多个文件打包成zip压缩文件以及对压缩文件的加密
- 用java代码实现文件的zip压缩与解压
- java实现多个文件压缩成压缩包
- Java实现压缩一个目录下所有图片文件
- java实现zip压缩文件/文件夹
- Java实现文件压缩与解压[zip格式,gzip格式]
- JAVA实现把指定文件夹下的所有文件压缩成zip包
- java实现单个或多个文件的压缩、解压缩 支持zip、rar等格式
- JAVA自己实现的文件压缩解压
- java 压缩打包文件的实现方法
- JAVA 实现压缩与解压缩 rar和zip格式的文件
- Java实现文件压缩与解压[zip格式,gzip格式]
- JAVA实现压缩文件或文件夹