package com.example.demo.utils;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* @author :sohikoryuu
* @date :Created in 2020/7/8 10:22
* @description:文件压缩与解压缩
* @version: 1.0
*/
public class FileZipUtils {
/**
* zip文件压缩
*
* @param inputFile 待压缩文件夹/文件名
* @param outputFile 生成的压缩包名字
*/
public static void ZipCompress(String inputFile, String outputFile) throws Exception {
//创建zip输出流
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFile));
//创建缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(out);
File input = new File(inputFile);
compress(out, bos, input, null);
bos.close();
out.close();
}
/**
* @param name 压缩文件名,可以写为null保持默认
*/
//递归压缩
public static void compress(ZipOutputStream out, BufferedOutputStream bos, File input, String name) throws IOException {
if (name == null) {
name = input.getName();
}
//如果路径为目录(文件夹)
if (input.isDirectory()) {
//取出文件夹中的文件(或子文件夹)
File[] flist = input.listFiles();
if (flist.length == 0) {//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入
out.putNextEntry(new ZipEntry(name + "/"));
} else {//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
for (int i = 0; i < flist.length; i++) {
compress(out, bos, flist[i], name + "/" + flist[i].getName());
}
}
} else {//如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
out.putNextEntry(new ZipEntry(name));
FileInputStream fos = new FileInputStream(input);
BufferedInputStream bis = new BufferedInputStream(fos);
int len = -1;
//将源文件写入到zip文件中
byte[] buf = new byte[1024];
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
bis.close();
fos.close();
}
}
/**
* zip解压
*
* @param inputFile 待解压文件名
* @param destDirPath 解压路径
*/
public static void ZipUncompress(String inputFile, String destDirPath) throws Exception {
File srcFile = new File(inputFile);//获取当前压缩文件
// 判断源文件是否存在
if (!srcFile.exists()) {
throw new Exception(srcFile.getPath() + "所指文件不存在");
}
//开始解压
//构建解压输入流
ZipInputStream zIn = new ZipInputStream(new FileInputStream(srcFile));
ZipEntry entry = null;
File file = null;
while ((entry = zIn.getNextEntry()) != null) {
if (!entry.isDirectory()) {
file = new File(destDirPath, entry.getName());
if (!file.exists()) {
new File(file.getParent()).mkdirs();//创建此文件的上级目录
}
OutputStream out = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(out);
int len = -1;
byte[] buf = new byte[1024];
while ((len = zIn.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 关流顺序,先打开的后关闭
bos.close();
out.close();
}
}
}
public static void main(String[] args) {
try {
// 文件夹压缩与解压缩
long start = System.currentTimeMillis();
ZipCompress("D:\\sohikoryuu\\material\\log.txt", "D:\\sohikoryuu\\test\\zip\\material.zip");
ZipUncompress("D:\\sohikoryuu\\test\\zip\\material.zip", "D:\\sohikoryuu\\test\\zip\\解压文件");
System.out.println((System.currentTimeMillis() - start));
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.cdel.qywechat.bussiniss;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.*;
import java.util.concurrent.CompletableFuture;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import static org.aspectj.weaver.tools.cache.ZippedFileCacheBacking.ZIP_FILE;
/**
* @author:suyanlong
* @date:Created in 2020/9/18 9:56
* @description: https://mp.weixin.qq.com/s/Q_PlYOvaB07Wtb2lQmk0aA
* @version: 1.0
*/
public class FileZip {
static String JPG_FILE = "D:\\suyl\\test\\73631636fd95364c0d83ca913ecea42.jpg";
static String FILE_NAME = "图片";
static String SUFFIX_FILE = ".jpg";
static long FILE_SIZE = new File(JPG_FILE).length();
public static void main(String[] args) {
zipFileNoBuffer();
zipFileBuffer();
zipFileChannel();
zipFileMap();
zipFilePip();
}
public static void zipFileNoBuffer() {
File zipFile = new File(ZIP_FILE);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile))) {
//开始时间
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
try (InputStream input = new FileInputStream(JPG_FILE)) {
zipOut.putNextEntry(new ZipEntry(FILE_NAME + i));
int temp = 0;
while ((temp = input.read()) != -1) {
zipOut.write(temp);
}
}
}
printInfo(beginTime);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void zipFileBuffer() {
File zipFile = new File(ZIP_FILE);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOut)) {
//开始时间
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(JPG_FILE))) {
zipOut.putNextEntry(new ZipEntry(FILE_NAME + i));
int temp = 0;
while ((temp = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(temp);
}
}
}
printInfo(beginTime);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void zipFileChannel() {
//开始时间
long beginTime = System.currentTimeMillis();
File zipFile = new File(ZIP_FILE);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
for (int i = 0; i < 10; i++) {
try (FileChannel fileChannel = new FileInputStream(JPG_FILE).getChannel()) {
zipOut.putNextEntry(new ZipEntry(i + SUFFIX_FILE));
fileChannel.transferTo(0, FILE_SIZE, writableByteChannel);
}
}
printInfo(beginTime);
} catch (Exception e) {
e.printStackTrace();
}
}
//Version 4 使用Map映射文件
public static void zipFileMap() {
//开始时间
long beginTime = System.currentTimeMillis();
File zipFile = new File(ZIP_FILE);
try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
WritableByteChannel writableByteChannel = Channels.newChannel(zipOut)) {
for (int i = 0; i < 10; i++) {
zipOut.putNextEntry(new ZipEntry(i + SUFFIX_FILE));
//内存中的映射文件
MappedByteBuffer mappedByteBuffer = new RandomAccessFile(JPG_FILE, "r").getChannel().map(
FileChannel.MapMode.READ_ONLY, 0, FILE_SIZE);
writableByteChannel.write(mappedByteBuffer);
}
printInfo(beginTime);
} catch (Exception e) {
e.printStackTrace();
}
}
//Version 5 使用Pip
public static void zipFilePip() {
long beginTime = System.currentTimeMillis();
try (WritableByteChannel out = Channels.newChannel(new FileOutputStream(ZIP_FILE))) {
Pipe pipe = Pipe.open();
//异步任务
CompletableFuture.runAsync(() -> runTask(pipe));
//获取读通道
ReadableByteChannel readableByteChannel = pipe.source();
ByteBuffer buffer = ByteBuffer.allocate(((int) FILE_SIZE) * 10);
while (readableByteChannel.read(buffer) >= 0) {
buffer.flip();
out.write(buffer);
buffer.clear();
}
} catch (Exception e) {
e.printStackTrace();
}
printInfo(beginTime);
}
//异步任务
public static void runTask(Pipe pipe) {
try (ZipOutputStream zos = new ZipOutputStream(Channels.newOutputStream(pipe.sink()));
WritableByteChannel out = Channels.newChannel(zos)) {
System.out.println("Begin");
for (int i = 0; i < 10; i++) {
zos.putNextEntry(new ZipEntry(i + SUFFIX_FILE));
FileChannel jpgChannel = new FileInputStream(new File(JPG_FILE)).getChannel();
jpgChannel.transferTo(0, FILE_SIZE, out);
jpgChannel.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void printInfo(long beginTime) {
System.out.println("====" + (System.currentTimeMillis() - beginTime) + "ms");
}
}
struts2中的参数封装静态参数封装什么是静态参数? 静态参数就是硬编码的,不可随意改变。 例子: 我们首先创建一个Action类,里面有两个参数,用来封装请求参数 public class User extends ActionSupport { private String username; //用户名
Responsive Web Design 响应式网页设计1、常见的布局方案固定布局:以像素作为页面的基本单位,不管设备屏幕及浏览器宽度,只设计一套尺寸;可切换的固定布局:同样以像素作为页面单位,参考主流设备尺寸,设计几套不同宽度的布局。通过识别的屏幕尺寸或浏览器宽度,选择最合适的那套宽度布局;弹性布局:以百分比作为页面的基本单位,可以适应一定范围内所有尺寸的设备屏幕及浏览器宽度,...
ABP提供了用于处理Web应用程序异常的标准模型.自动处理所有异常.如果是API/AJAX请求,会向客户端返回一个标准格式化后的错误消息. 自动隐藏内部详细错误并返回标准错误消息. 为异常消息的本地化提供一种可配置的方式. 自动为标准异常设置HTTP状态代码,并提供可配置选项,以映射自定义异常.自动处理异常当满足下面任意一个条件时,AbpExceptionFilt...
java执行Sql脚本
如题,最近遇到一些客户反馈回来的问题。截了个图过来,我一看字体完全撑大了我的LinearLayout 百思不得其解,作为一个专业的码农,我发誓这个代码肯定不是我写了,休想让我背锅。原因是用户设置了系统的字体所以导致偏好导致字体撑大,so 今天来解决一下这个问题。谷歌官方教材建议的是采用sp做文字的单位。但是在实际开发的时候回发现一个问题,比如在标题布局的时候一般都会使用40dp或者50dp和32s...
使用Delphi控制条码打印机打印条码(系列问题1) Delphi / Windows SDK/APIhttp://www.delphi2007.net/DelphiNetwork/html/delphi_20061204150648203.html使用Delphi程序控制专用条码打印机打印带有条码的卡片(卡片可以贴在商品上)。本人第一次涉及此类问题,是新手,看了相关的问题列表,没有很清楚的...
谷歌地图 街景 apiSince launching in May of 2007, Google Street View has since expanded coverage to enough cities across the world that Google now feels comfortable integrating it more deeply with their Maps...
目的:在微信小程序内实现一张中国地图,上面要写上一些全国数据,点击省 => 省地图,并请求数据,点击市区 => 跳转到市区地图这样一个功能,具体实现效果如下:思路:1.首先我们要先画一张中国地图,直接用最近版的echarts-for-weixin插件,下载完之后吧echart组件放到小程序根目录,可以看看里面的使用方法,也可以下载它的案例来看,但是里面是一个省份地图,所以...
有过好几次了,写完的博客点击下面的发表文章,没有反应,然后再写新文章,前面写的就丢了。 刚写过的没了, 写博客往往是有感而发, 刚才写的东西过一会,感觉过去了,就不想再写了,所以丢了就丢了,不会再想写了。 所以,今天早上就先到这吧,也许晚上会再想写吧。 刚才把牛肉炖了, 中午吃牛肉。 今天感觉精神比较爽,昨晚上踢了2小时的球,虽然膝盖还有点
我们都知道,业务开发涉及到数据库的SQL操作时,一定要 review 是否命中索引。否则,会走 全表扫描,如果表数据量很大时,会慢的要死。假如命中了索引呢?是不是就不会有慢查询?殊不知,我...
计算机组成原理第四章_作业_2012-13-1 (9页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦!19.90 积分4.14 ROM(2K*8位,4K*4位,8K*8位) RAM(1K*4位,2K*8位,4K*8位) 系统程序区:最小4K 地址范围:0~4K-1 用户程序区:4096~16383 地址范围:4k~16K-1 解: (1) 写出对应的二进...