java解压


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


import org.apache.log4j.Logger;




/**
 * 压缩包工具类
 */
public class ZipUtil {
private static final Logger logger = Logger.getLogger(ZipUtil.class);
/**
* 解压zip文件 
* @param zipFile
* @param unzipFilePath 解压后存放的路径
* @return 返回解压的路径文件夹
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static String unzip(File zipFile, String unzipFilePath) throws Exception{  
logger.trace("【update】解压文件:"+zipFile+"到路径:"+unzipFilePath);
String unzipPath = "";
//判断文件是否存在
if(!zipFile.exists() || zipFile.length()<=0){
unzipPath="false";
return unzipPath;
}
if(zipFile.length()<=0){
unzipPath="false";
return unzipPath;
}
//创建解压缩文件保存的路径  
File unzipFileDir = new File(unzipFilePath);  
//的判断文件夹是否存在如果存在则不创建 如果不存在 则创建
if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()){  
//创建文件夹
unzipFileDir.mkdirs();  
}
//开始解压  
logger.trace("开始解压....");
//创建解压对象
ZipEntry zipEntry = null;  
//文件保存路径路径
String entryFilePath = null;
//文件夹路径
String entryDirPath = null; 
//创建问价对象
File entryFile = null;
//创建文件夹对象
File entryDir = null;  
int index = 0, count = 0, bufferSize = 1024;  
byte[] buffer = new byte[bufferSize];  
//创建输出字符流
BufferedInputStream bufferedInputStream = null;  
//创建输入字符流
BufferedOutputStream bufferedOutputStream = null; 
try {


//创建压缩文件对象
ZipFile zip = new ZipFile(zipFile);  
Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zip.entries();  
//第一步循环创建文件夹 第二步创建文件 第三部写入文件
while(entries.hasMoreElements()){  
zipEntry = entries.nextElement();  
logger.debug("当前的zip对象zipEntry:"+zipEntry.getName());
logger.debug("当前解压路径unzipFilePath:"+unzipFilePath);
boolean isDir = zipEntry.isDirectory();
//当前文件为文件夹
if(isDir){
logger.debug("当前是个文件夹..."+zipEntry.getName());
String dir = zipEntry.getName();
entryFilePath = unzipFilePath + dir + "//";  
logger.debug("当前文件夹的完整路径是:"+entryFilePath );
//定义文件夹
entryDir = new File(entryFilePath); 
//如果文件夹路径不存在,则创建文件夹  
if (!entryDir.exists() || !entryDir.isDirectory()){  
entryDir.mkdirs();  
logger.debug("创建文件夹:"+entryFilePath);
}
}else{
//当前是个文件
logger.debug("判断当前是个文件:"+zipEntry.getName());
entryFilePath = unzipFilePath + zipEntry.getName();  
logger.debug("当前文件的完整路径是entryFilePath:"+entryFilePath);
File f = new File(entryFilePath);
if (index != -1){  
entryDirPath = f.getAbsolutePath().split(f.getName())[0]; 
}else{  
entryDirPath = "";  
}   
logger.debug("entryDirPath:"+entryDirPath);
unzipPath = entryDirPath;
//定义文件夹
entryDir = new File(entryDirPath);  
//如果文件夹路径不存在,则创建文件夹  
if (!entryDir.exists() || !entryDir.isDirectory()){  
entryDir.mkdirs();  
}  
//创建解压文件  
entryFile = new File(entryFilePath);  
//写入文件  
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(entryFile));  
//读取文件
bufferedInputStream = new BufferedInputStream(zip.getInputStream(zipEntry));  
//文件写入
while ((count = bufferedInputStream.read(buffer, 0, bufferSize)) != -1){  
bufferedOutputStream.write(buffer, 0, count);  
}  
bufferedOutputStream.flush();  
bufferedOutputStream.close(); 
}

logger.debug("文件解压完毕...重新组装的路径是:"+unzipPath);
}finally{
try {
if(null != bufferedInputStream){
bufferedInputStream.close();
}
if(null != bufferedOutputStream){
bufferedOutputStream.close();
}
} catch (Exception e2) {
}
}
return unzipPath;
}

public static void main(String[] args){
File zipFile = new File("D:\\P01.000243.Z");
System.out.println(zipFile.getName());

try {
// CompressorInputStream //zStreamFactory.getCompressorStream(null);
// zStreamFactory.getCompressorStream(new FileInputStream(new File("D://P01.000243.Z")));
// boolean result = zipFile.renameTo(file);
// System.out.println(result);
// ZipUtil.unzip(file, "C://");
System.out.println("解压完成!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,我们可以知道Java解压JPEG图片需要使用OpenCV库。下面是一个Java解压JPEG图片的例子: ```java import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.MatOfInt; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class JPEGDecompression { public static void main(String[] args) { // 加载OpenCV库 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // 读取JPEG图片 Mat image = Imgcodecs.imread("test.jpg"); // 将图片转换为灰度图像 Mat grayImage = new Mat(); Imgproc.cvtColor(image, grayImage, Imgproc.COLOR_BGR2GRAY); // 对灰度图像进行高斯模糊 Mat blurredImage = new Mat(); Imgproc.GaussianBlur(grayImage, blurredImage, new Size(5, 5), 0); // 对模糊后的图像进行Canny边缘检测 Mat edges = new Mat(); Imgproc.Canny(blurredImage, edges, 0, 50); // 将边缘检测结果转换为BGR图像 MatOfByte matOfByte = new MatOfByte(); Imgcodecs.imencode(".jpg", edges, matOfByte); byte[] byteArray = matOfByte.toArray(); Mat mat = Imgcodecs.imdecode(new MatOfByte(byteArray), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED); // 将BGR图像保存为JPEG图片 MatOfInt params = new MatOfInt(Imgcodecs.IMWRITE_JPEG_QUALITY, 100); Imgcodecs.imwrite("output.jpg", mat, params); } } ``` 上述代码中,我们首先加载了OpenCV库,然后使用Imgcodecs.imread()方法读取了JPEG图片。接着,我们将图片转换为灰度图像,并对灰度图像进行高斯模糊和Canny边缘检测。最后,我们将边缘检测结果转换为BGR图像,并使用Imgcodecs.imwrite()方法将BGR图像保存为JPEG图片。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值