java上传文件,压缩,解压保存
来源:互联网 发布:mac qq接收不了文件 编辑:程序博客网 时间:2024/06/04 17:41
<pre name="code" class="html">import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Enumeration;import java.util.List;import org.apache.tools.zip.ZipEntry;import org.apache.tools.zip.ZipFile;import org.apache.tools.zip.ZipOutputStream;import com.xbrc.common.misc.OperationException;import com.xbrc.common.misc.OperationException.ExceptionType;
/** * 打包指定文件列表为zip * * @param files * @param strZipFileFullPath * @throws Exception */ public static void zipFiles(String strZipFileFullPath, List<File> files) { try { File file = new File(strZipFileFullPath); if (!file.exists()) { file.createNewFile(); } FileOutputStream fous = new FileOutputStream(file); ZipOutputStream zipOut = new ZipOutputStream(fous); zipFile(files, zipOut); zipOut.close(); fous.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * 解压 * * @param directory * 解压完保存的路径 * @param zip * 压缩包文件 */
public static void unZipFile(String directory, File zip) { try { int count = -1; int buffer = 2048; File file = null; InputStream is = null; FileOutputStream fos = null; BufferedOutputStream bos = null; ZipFile zipFile = new ZipFile(zip, "GBK"); Enumeration en = zipFile.getEntries(); createFolder(directory); while (en.hasMoreElements()) { byte buf[] = new byte[buffer]; ZipEntry entry = (ZipEntry) en.nextElement(); String fileName = entry.getName(); String filePath = directory +"/"+ fileName; if(entry.isDirectory()){ createFolder(filePath); }else{ file = new File(filePath); file.createNewFile(); is = zipFile.getInputStream(entry); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos, buffer); while ((count = is.read(buf)) > -1) { bos.write(buf, 0, count); } bos.close(); fos.close(); is.close(); } } zipFile.close(); } catch (IOException e) { e.printStackTrace(); } }
private static void createFolder(String path) { try { File uploadFilePath = new File(path); if (uploadFilePath.exists() == false) { uploadFilePath.mkdirs(); } } catch (Exception ex) { throw new OperationException(ExceptionType.ERROR, "创建文件夹异常"); } }
0 0
- java上传文件,压缩,解压保存
- 文件上传、压缩解压 操作
- 文件下载,上传,压缩,解压
- JAVA 压缩解压文件
- JAVA文件压缩、解压
- 压缩解压文件--Java
- java压缩,解压文件
- springMVC 上传压缩包文件、解压
- 文件上传压缩包并解压
- java 压缩、解压文件、文件夹。
- java压缩与解压文件
- Java文件解压与压缩
- Java压缩解压文件/文件夹
- JAVA压缩及解压文件
- java压缩和解压文件
- java文件压缩与解压
- java 解压,压缩单个文件
- java压缩解压ZIP文件
- 程序内存分布分析
- java url 传参 编码
- 第十五章 15.4节练习
- SpringMVC常用基础知识【转】
- 两种方法int型转16进制
- java上传文件,压缩,解压保存
- java 求两个数的公约数和公倍数
- android +7与8开头的号码在拨号盘上搜索相互匹配
- IOS算法(五)之折半查找
- 实现LRU CRU
- android 匈牙利语环境下,联系人的姓名顺序颠倒
- 1.认识服务器推送技术
- 屏蔽Home按键
- java正则表达式