Commit bb791662 authored by 张永's avatar 张永

add 删除指定后缀文件

parent 637e5fec
......@@ -377,6 +377,16 @@ public class FileUtil {
}
}
}
public static void fileDeleteBySuffix(String filePath,String suffix) {
File file = new File(filePath);
File[] files = file.listFiles();
for(File f:files) {
if(f.getName().endsWith(suffix)) {
f.delete();
}
}
}
public static byte[] streamToBytes(InputStream instream) {
byte[] bytes = null;
......
package com.egolm.common;
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.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import com.egolm.common.exception.ZipUtilException;
public final class ZipUtil {
public static File zip(String path) {
return zip(new File(path));
}
public static File zip(File file) {
return zip(file, file.getAbsolutePath() + ".zip");
}
public static File zip(String path, String zipTo) {
return zip(new File(path), zipTo);
}
public static File zip(File file, String zipTo) {
File target = null;
if (file.exists()) {
target = FileUtil.createFile(zipTo);
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(target);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
addEntry(null, file, zos);
} catch (IOException e) {
throw new ZipUtilException(e);
} finally {
try {
if (zos != null) {
zos.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
throw new ZipUtilException(e);
}
}
}
return target;
}
private static void addEntry(String base, File source, ZipOutputStream zos) {
String entry = null;
if(StringUtil.isBlank(base)) {
entry = source.getName();
} else {
entry = base + File.separator + source.getName();
}
if (source.isDirectory()) {
for (File file : source.listFiles()) {
addEntry(entry, file, zos);
}
} else {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
try {
byte[] buffer = new byte[1024 * 10];
fis = new FileInputStream(source);
bis = new BufferedInputStream(fis, buffer.length);
int read = 0;
zos.putNextEntry(new ZipEntry(entry));
while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
zos.write(buffer, 0, read);
}
zos.closeEntry();
} catch (IOException e) {
throw new ZipUtilException(e);
} finally {
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
}
} catch (IOException e) {
throw new ZipUtilException(e);
}
}
}
public static void unzip(String zipName) {
File source = new File(zipName);
unzip(source, source.getParentFile().getAbsolutePath() + source.getName());
}
public static void unzip(File source) {
unzip(source, source.getParentFile().getAbsolutePath() + source.getName());
}
public static void unzip(String zipName, String targetPath) {
unzip(new File(zipName), targetPath);
}
public static void unzip(File file, String descDir) {
ZipFile zipFile = null;
ZipInputStream zis = null;
try {
zipFile = new ZipFile(file);
zis = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
while((entry = zis.getNextEntry()) != null && !entry.isDirectory()) {
InputStream is = zipFile.getInputStream(entry);
FileUtil.streamToFile(is, descDir + File.separator + entry.getName());
}
} catch (Exception e) {
throw new ZipUtilException(e);
} finally {
try {
if(zipFile != null) {
zipFile.close();
}
if(zis != null) {
zis.close();
}
} catch (Exception e) {
throw new ZipUtilException(e);
}
}
}
public static void main(String[] args) {
String targetPath = "D:\\Green\\IDC\\50010";
File file = ZipUtil.zip(targetPath);
ZipUtil.unzip(file, "D:\\IDC");
}
package com.egolm.common;
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.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import com.egolm.common.exception.ZipUtilException;
public final class ZipUtil {
public static File zip(String path) {
return zip(new File(path));
}
public static File zip(File file) {
return zip(file, file.getAbsolutePath() + ".zip");
}
public static File zip(String path, String zipTo) {
return zip(new File(path), zipTo);
}
public static File zip(File file, String zipTo) {
File target = null;
if (file.exists()) {
target = FileUtil.createFile(zipTo);
FileOutputStream fos = null;
ZipOutputStream zos = null;
try {
fos = new FileOutputStream(target);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
addEntry(null, file, zos);
} catch (IOException e) {
throw new ZipUtilException(e);
} finally {
try {
if (zos != null) {
zos.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
throw new ZipUtilException(e);
}
}
}
return target;
}
private static void addEntry(String base, File source, ZipOutputStream zos) {
String entry = null;
if(StringUtil.isBlank(base)) {
entry = source.getName();
} else {
entry = base + File.separator + source.getName();
}
if (source.isDirectory()) {
for (File file : source.listFiles()) {
addEntry(entry, file, zos);
}
} else {
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
try {
byte[] buffer = new byte[1024 * 10];
fis = new FileInputStream(source);
bis = new BufferedInputStream(fis, buffer.length);
int read = 0;
zos.putNextEntry(new ZipEntry(entry));
while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
zos.write(buffer, 0, read);
}
zos.closeEntry();
} catch (IOException e) {
throw new ZipUtilException(e);
} finally {
if (bis != null) {
bis.close();
}
if (fis != null) {
fis.close();
}
}
} catch (IOException e) {
throw new ZipUtilException(e);
}
}
}
public static void unzip(String zipName) {
File source = new File(zipName);
unzip(source, source.getParentFile().getAbsolutePath() + source.getName());
}
public static void unzip(File source) {
unzip(source, source.getParentFile().getAbsolutePath() + source.getName());
}
public static void unzip(String zipName, String targetPath) {
unzip(new File(zipName), targetPath);
}
public static void unzip(File file, String descDir) {
ZipFile zipFile = null;
ZipInputStream zis = null;
try {
zipFile = new ZipFile(file);
zis = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
while((entry = zis.getNextEntry()) != null && !entry.isDirectory()) {
InputStream is = zipFile.getInputStream(entry);
FileUtil.streamToFile(is, descDir + File.separator + entry.getName());
}
} catch (Exception e) {
throw new ZipUtilException(e);
} finally {
try {
if(zipFile != null) {
zipFile.close();
}
if(zis != null) {
zis.close();
}
} catch (Exception e) {
throw new ZipUtilException(e);
}
}
}
public static void main(String[] args) {
String targetPath = "D:\\data\\erp\\91310114594794273Y";
File file = ZipUtil.zip(targetPath);
System.out.println(file.getName());
//ZipUtil.unzip(file, "D:\\data\\erp\\91310114594794273Y");
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment