/**
* 解压缩zip文件
*/
public class ZipUtils {
public ZipUtils() {
}
/**
* 根据byte数组,生成文件
*/
public static void getFile(byte[] bfile, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists()) {//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
// 使用密码解压(图片不加密)
public static boolean unZipFile1(String zipFileFullName, String filePath, String password) {
try {
ZipFile zipFile = new ZipFile(zipFileFullName);
// 如果解压需要密码
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
zipFile.extractAll(filePath);//提取所有文件
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
// 使用密码解压
public static boolean unZipFile(String zipFileFullName, String filePath, String password) {
try {
ZipFile zipFile = new ZipFile(zipFileFullName);
// 如果解压需要密码
if (zipFile.isEncrypted()) {
zipFile.setPassword(password);
}
File file = new File(filePath);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
zipFile.extractAll(filePath);//提取所有文件
// 压缩
ZipFolder(filePath, filePath + "01");
// 解压
ZipUtils.UnZipFolder(filePath + "01", filePath);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 解压zip到指定的路径
*
* @param zipFileString ZIP的名称
* @param outPathString 要解压缩路径
* @throws Exception
*/
public static void UnZipFolder(String zipFileString, String outPathString) {
ZipInputStream inZip = null;
OutputStream out = null;
try{
inZip = new ZipInputStream(new FileInputStream(zipFileString));
ZipEntry zipEntry;
String szName = "";
List<File> fileList = new ArrayList<File>();
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
//获取部件的文件夹名
szName = szName.substring(0, szName.length() - 1);
File folder = new File(outPathString + File.separator + szName);
folder.mkdirs();
} else {
File file = new File(outPathString + File.separator + szName);
if (!file.exists()) {
fileList.add(file);
file.getParentFile().mkdirs();
file.createNewFile();
}
// 获取文件的输出流
// FileOutputStream out = new FileOutputStream(file);
out = AesUtil.encrypt(file, AesUtil.toKey(MyApplication.getInstance().getAESKey().getBytes()));// 加密
int len;
byte[] buffer = new byte[1024];
// 读取(字节)字节到缓冲区
while ((len = inZip.read(buffer)) != -1) {
// 从缓冲区(0)位置写入(字节)字节
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}
inZip.close();
// 删除目录下多余文件夹
File dirFile = new File(outPathString);
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
deleteDirectory(files[i].getAbsolutePath());
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(inZip != null){
safeClose(inZip);
}
if(out != null){
safeClose(out);
}
}
}
public static void safeClose(OutputStream fis){
if(fis != null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
public static void safeClose(ZipInputStream fis){
if(fis != null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
/**
* 压缩文件和文件夹
*
* @param srcFileString 要压缩的文件或文件夹
* @param zipFileString 解压完成的Zip路径
* @throws Exception
*/
public static void ZipFolder(String srcFileString, String zipFileString) {
FileOutputStream fis = null;
ZipOutputStream outZip = null;
try{
fis = new FileOutputStream(zipFileString);
//创建ZIP
outZip = new ZipOutputStream(fis);
//创建文件
File file = new File(srcFileString);
//压缩
ZipFiles(file.getParent() + File.separator, file.getName(), outZip);
//完成和关闭
outZip.finish();
outZip.close();
fis.close();
}catch (Exception e){
e.printStackTrace();
}finally {
if(fis != null){
safeClose(fis);
}
if(outZip != null){
safeClose1(outZip);
}
}
}
public static void safeClose(FileOutputStream fis){
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void safeClose1(ZipOutputStream fis){
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 压缩文件
*
* @param folderString
* @param fileString
* @param zipOutputSteam
* @throws Exception
*/
private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) {
FileInputStream inputStream = null;
try{
if (zipOutputSteam == null)
return;
File file = new File(folderString + fileString);
if (file.isFile()) {
ZipEntry zipEntry = new ZipEntry(fileString);
inputStream = new FileInputStream(file);
zipOutputSteam.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[4096];
while ((len = inputStream.read(buffer)) != -1) {
zipOutputSteam.write(buffer, 0, len);
}
zipOutputSteam.closeEntry();
} else {
//文件夹
String fileList[] = file.list();
//没有子文件和压缩
if (fileList.length <= 0) {
ZipEntry zipEntry = new ZipEntry(fileString + File.separator);
zipOutputSteam.putNextEntry(zipEntry);
zipOutputSteam.closeEntry();
}
//子文件和递归
for (int i = 0; i < fileList.length; i++) {
ZipFiles(folderString + fileString + "/", fileList[i], zipOutputSteam);
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(inputStream != null){
safeClose(inputStream);
}
}
}
public static Bitmap getBitmap(File photoFile) {
InputStream fis = null;
try {
fis = AesUtil.decrypt(photoFile, AesUtil.toKey(MyApplication.getInstance().getAESKey().getBytes()));
return BitmapFactory.decodeStream(fis); ///把流转化为Bitmap图片
} catch (FileNotFoundException e) {
e.printStackTrace();
MyLog.e("mylog", "e1:" + e.getMessage());
return null;
} catch (Exception e) {
e.printStackTrace();
MyLog.e("mylog", "e2:" + e.getMessage());
return null;
} finally {
if(fis != null){
safeClose(fis);
}
}
}
public static void safeClose(InputStream fis){
if(fis != null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
public static File getPhotoFile(String nonet, String fileAbsolutePath) {
File file = new File(fileAbsolutePath);
File[] subFile = file.listFiles();
if (subFile != null) {
for (int i = 0; i < subFile.length; i++) {
// 判断是否为文件夹
/*if (subFile[i].isDirectory()) {
getPhotoFile(idNonet, subFile[i].getAbsolutePath());
} else {*/
String filename = subFile[i].getName();
if (!TextUtils.isEmpty(filename) && filename.length() >= 13 && nonet != null) {
String subFilename = filename.substring(filename.length() - 13, filename.length() - 4);
// MyLog.e("mylog", "subFilename:" + subFilename + " nonet:" + nonet);
if (subFilename.equals(nonet)) {
MyLog.e("mylog", "filename:" + filename);
return subFile[i];
}
}
}
}
return null;
}
/**
* @param zipName 压缩文件的路径
* @param filePath 被压缩文件的路径
* @param password 加密
* @description:压缩以及加密
* @author: renbo
* @date: 2021年5月19日 下午3:35:33
*/
public static void unZipPass(String zipName, String filePath, String password) throws ZipException {
ZipFile zipFile = new ZipFile(zipName);
ArrayList<File> filesToAdd = new ArrayList<File>();
File root = new File(filePath);
File[] files = root.listFiles();
for (File file : files) {
if (file.isDirectory()) {
filesToAdd.add(new File(file.getAbsolutePath()));
} else {
filesToAdd.add(new File(file.getAbsolutePath()));
}
}
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
// Set password
parameters.setPassword(password);
zipFile.addFiles(filesToAdd, parameters);
}
/**
* 删除单个文件
*
* @param filePath 被删除文件的文件名
* @return 文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
return file.delete();
}
return false;
}
/**
* 删除文件夹以及目录下的文件
*
* @param filePath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String filePath) {
boolean flag = false;
//如果filePath不以文件分隔符结尾,自动添加文件分隔符
if (!filePath.endsWith(File.separator)) {
filePath = filePath + File.separator;
}
File dirFile = new File(filePath);
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
flag = true;
File[] files = dirFile.listFiles();
//遍历删除文件夹下的所有文件(包括子目录)
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
//删除子文件
flag = deleteFile(files[i].getAbsolutePath());
if (!flag) break;
} else {
//删除子目录
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag) break;
}
}
if (!flag) return false;
//删除当前空目录
return dirFile.delete();
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*
* @param filePath 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public static boolean DeleteFolder(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return false;
} else {
if (file.isFile()) {
// 为文件时调用删除文件方法
return deleteFile(filePath);
} else {
// 为目录时调用删除目录方法
return deleteDirectory(filePath);
}
}
}
}
|