18.zip
发表日期:2022-08-05 16:22:18 | 来源: | | 浏览(610) 分类:JAVA基础
ZipDemo01
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipDemo01 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File file = new File("d:"+File.separator+"test.txt"); File zipFile = new File("d:"+File.separator+"test.zip"); FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); zos.setComment("这里是注释"); ZipEntry zEntry = new ZipEntry(file.getName()); zos.putNextEntry(zEntry); int temp = 0; while ((temp=fis.read())!=-1) { zos.write(temp); } zos.finish() ; zos.close(); } private static Object Charset(String string) { // TODO Auto-generated method stub return null; } }
ZipUtils
/** * Created by Administrator on 2016/4/5. */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; public class ZipUtils { public static void zip(ArrayList<String> src, String dest) throws IOException { ZipOutputStream out = null; try { File outFile = new File(dest);// 源文件或者目录 out = new ZipOutputStream(new FileOutputStream(outFile)); for (int i = 0; i < src.size(); i++) { File fileOrDirectory = new File(src.get(i));// 压缩文件路径 zipFileOrDirectory(out, fileOrDirectory, ""); } } catch (IOException ex) { ex.printStackTrace(); } finally { // 关闭输出流 if (out != null) { try { out.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } public static void zip(String src, String dest) throws IOException { // 提供了一个数据项压缩成一个ZIP归档输出流 ZipOutputStream out = null; try { File outFile = new File(dest);// 源文件或者目录 File fileOrDirectory = new File(src);// 压缩文件路径 out = new ZipOutputStream(new FileOutputStream(outFile)); // 如果此文件是一个文件,否则为false。 if (fileOrDirectory.isFile()) { zipFileOrDirectory(out, fileOrDirectory, ""); } else {// 返回一个文件或空阵列。 File[] entries = fileOrDirectory.listFiles(); for (int i = 0; i < entries.length; i++) { // 递归压缩,更新curPaths zipFileOrDirectory(out, entries[i], ""); } } } catch (IOException ex) { ex.printStackTrace(); } finally { // 关闭输出流 if (out != null) { try { out.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } private static void zipFileOrDirectory(ZipOutputStream out, File fileOrDirectory, String curPath) throws IOException { // 从文件中读取字节的输入流 FileInputStream in = null; try { // 如果此文件是一个目录,否则返回false。 if (!fileOrDirectory.isDirectory()) { // 压缩文件 byte[] buffer = new byte[4096]; int bytes_read; in = new FileInputStream(fileOrDirectory); // 实例代表一个条目内的ZIP归档 ZipEntry entry = new ZipEntry(curPath + fileOrDirectory.getName()); // 条目的信息写入底层流 out.putNextEntry(entry); while ((bytes_read = in.read(buffer)) != -1) { out.write(buffer, 0, bytes_read); } out.closeEntry(); } else { // 压缩目录 File[] entries = fileOrDirectory.listFiles(); for (int i = 0; i < entries.length; i++) { // 递归压缩,更新curPaths zipFileOrDirectory(out, entries[i], curPath + fileOrDirectory.getName() + "/"); } } } catch (IOException ex) { ex.printStackTrace(); // throw ex; } finally { if (in != null) { try { in.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } @SuppressWarnings("unchecked") public static void unzip(String zipFileName, String outputDirectory) throws IOException { ZipFile zipFile = null; try { zipFile = new ZipFile(zipFileName); Enumeration e = zipFile.entries(); ZipEntry zipEntry = null; File dest = new File(outputDirectory); dest.mkdirs(); while (e.hasMoreElements()) { zipEntry = (ZipEntry) e.nextElement(); String entryName = zipEntry.getName(); InputStream in = null; FileOutputStream out = null; try { if (zipEntry.isDirectory()) { String name = zipEntry.getName(); name = name.substring(0, name.length() - 1); File f = new File(outputDirectory + File.separator + name); f.mkdirs(); } else { int index = entryName.lastIndexOf("\\"); if (index != -1) { File df = new File(outputDirectory + File.separator + entryName.substring(0, index)); df.mkdirs(); } index = entryName.lastIndexOf("/"); if (index != -1) { File df = new File(outputDirectory + File.separator + entryName.substring(0, index)); df.mkdirs(); } File f = new File(outputDirectory + File.separator + zipEntry.getName()); // f.createNewFile(); in = zipFile.getInputStream(zipEntry); out = new FileOutputStream(f); int c; byte[] by = new byte[1024]; while ((c = in.read(by)) != -1) { out.write(by, 0, c); } out.flush(); } } catch (IOException ex) { ex.printStackTrace(); throw new IOException("解压失败:" + ex.toString()); } finally { if (in != null) { try { in.close(); } catch (IOException ex) { } } if (out != null) { try { out.close(); } catch (IOException ex) { } } } } } catch (IOException ex) { ex.printStackTrace(); throw new IOException("解压失败:" + ex.toString()); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException ex) { } } } } } /* * Activity调用 package com.comc; import java.io.IOException; import * com.zipUtil.ZipUtil; import android.app.Activity; import android.os.Bundle; * public class IZipActivity extends Activity { * * * @Override public void onCreate(Bundle savedInstanceState) { * super.onCreate(savedInstanceState); setContentView(R.layout.main); try { * ZipUtil.zip("/data/data/com.comc/databases", * "/data/data/com.comc/databases.zip"); * ZipUtil.unzip("/data/data/com.comc/databases.zip", * "/data/data/com.comc/databases"); } catch (IOException e) { * e.printStackTrace(); } } * * } */
import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Demo { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<String> fileListsList = new ArrayList<String>(); fileListsList.add("D:/zip/device-2016-03-26-195134.png"); fileListsList.add("D:/demo/hyasset/config.xml"); fileListsList.add("D:/demo.txt"); fileListsList.add("D:/demosvn/YCCitizen/build/intermediates/dex-cache/"); try { ZipUtils.zip(fileListsList, "D:/ziped/abc.zip"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* try { ZipUtils.zip("D:/zip/", "D:/ziped/abc.zip"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } */ } }
- JAVA(0)
- JAVA基础(30)
- 1.JAVA开发环境配置(0)
- 2.java数据类型(0)
- 3.数组(0)
- 4.Date(0)
- 5.String 和 StringBuffer类常用方法(0)
- 6.Math类(0)
- 7.Cloneable(0)
- 8.File 文件(0)
- 9.FileReader和FileWriter(0)
- 10.RandomAccessFile(0)
- 11.FileInputStream和FileOutputStream(0)
- 12.InputStreamReader和OutputStreamWriter(0)
- 13.BufferedReader(0)
- 14.Scanner(0)
- 15.DataOutputStream(0)
- 16.Thead 多线程(0)
- 17.TimerTask(0)
- 18.zip(0)
- 19.Charset(0)
- 20.List(0)
- 21.Map(0)
- 22.Properties(0)
- 23.Enumeration(0)
- 24.Collection(0)
- 25.JDBC(0)
- 26.Iterator(0)
- 27.Abstract(0)
- 28.Interface(0)
- 29.Serializable(0)
- 30.Camparable(0)
- JSP基础(50)