代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
压缩和解压缩
作者:
飞飞爱青青
/ 发布于
2013/2/1
/
706
使用java提供的基础功能实现压缩和解压缩,不支持中文
package com.yfei.uti.zip; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Stack; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; /** * ?java基本zip压缩。不支持文件名和路径中有中文 * * @author yfei * */ public class ZipUtil { /** * Creates the new file. * * @param fFile * the f file * @throws Exception * the exception */ private static void createNewFile(File fFile) throws Exception { if (fFile.isDirectory()) { createNewFolder(fFile); } else { File fParent = fFile.getParentFile(); createNewFolder(fParent); boolean b = false; b = fFile.createNewFile(); } } /** * Creates the new folder. * * @param fObj * the f obj * @throws Exception * the exception */ private static void createNewFolder(File fObj) throws Exception { Stack<File> fList = new Stack<File>(); if (fObj.exists()) { return; } searchNewFolder(fObj, fList); while (fList.size() > 0) { File fTemp = fList.pop(); boolean b = false; b = fTemp.mkdir(); } } /** * Search new folder. * * @param fObj * the f obj * @param allFolder * the all folder * @throws Exception * the exception */ private static void searchNewFolder(File fObj, Stack<File> allFolder) throws Exception { if (fObj.exists()) { return; } else { allFolder.push(fObj); File fParent = fObj.getParentFile(); searchNewFolder(fParent, allFolder); } } /** * Unzip. * * @param zipFileName * the zip file name * @param outputDirectory * the output directory * @throws Exception * the exception */ public static void unzip(String zipFileName, String outputDirectory) throws Exception { ZipInputStream in = null; FileOutputStream out = null; FileInputStream fileIn = null; try { fileIn = new FileInputStream(zipFileName); in = new ZipInputStream(fileIn); ZipEntry z; while ((z = in.getNextEntry()) != null) { if (z.isDirectory()) { String name = z.getName(); name = name.substring(0, name.length() - 1); File f = new File(outputDirectory + File.separator + name); boolean b = false; b = f.mkdir(); } else { File f = new File(outputDirectory + File.separator + z.getName()); // f.createNewFile(); createNewFile(f); out = new FileOutputStream(f); int b; while ((b = in.read()) != -1) { out.write(b); } } } } catch (Exception ex) { throw ex; } finally { in.close(); } } /** * Zip. * * @param zipFileName * the zip file name * @param inputFile * the input file * @throws Exception * the exception */ public static void zip(String zipFileName, File inputFile) throws Exception { ZipOutputStream out = null; try { out = new ZipOutputStream(new FileOutputStream(zipFileName)); zip(out, inputFile, ""); } catch (Exception ex) { throw ex; } finally { out.close(); } } /** * Zip. * * @param zipFileName * the zip file name * @param inputFile * the input file * @throws Exception * the exception */ public static void zip(String zipFileName, String inputFile) throws Exception { zip(zipFileName, new File(inputFile)); } /** * Zip. * * @param out * the out * @param f * the f * @param base * the base * @throws Exception * the exception */ public static void zip(ZipOutputStream out, File f, String base) throws Exception { FileInputStream in = null; if (f.isDirectory()) { File[] fl = f.listFiles(); out.putNextEntry(new ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < fl.length; i++) { File fff = fl[i]; zip(out, fff, base + fff.getName()); } } else { // if (base == null || base != null && base.equals("")) { if ((base == null) || "".equals(base)) { base = f.getName(); } } out.putNextEntry(new ZipEntry(base)); try { in = new FileInputStream(f); int b; while ((b = in.read()) != -1) { out.write(b); } } catch (FileNotFoundException exTemp) { throw exTemp; } finally { in.close(); } } public static void main(String args[]) throws Exception { unzip( "D:\\tender1.zip", "D:\\"); } }
试试其它关键字
压缩和解压缩
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
飞飞爱青青
贡献的其它代码
(
1
)
.
压缩和解压缩
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3