Commit bb791662 authored by 张永's avatar 张永

add 删除指定后缀文件

parent 637e5fec
...@@ -377,6 +377,16 @@ public class FileUtil { ...@@ -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) { public static byte[] streamToBytes(InputStream instream) {
byte[] bytes = null; byte[] bytes = null;
......
package com.egolm.common; package com.egolm.common;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import com.egolm.common.exception.ZipUtilException; import com.egolm.common.exception.ZipUtilException;
public final class ZipUtil { public final class ZipUtil {
public static File zip(String path) { public static File zip(String path) {
return zip(new File(path)); return zip(new File(path));
} }
public static File zip(File file) { public static File zip(File file) {
return zip(file, file.getAbsolutePath() + ".zip"); return zip(file, file.getAbsolutePath() + ".zip");
} }
public static File zip(String path, String zipTo) { public static File zip(String path, String zipTo) {
return zip(new File(path), zipTo); return zip(new File(path), zipTo);
} }
public static File zip(File file, String zipTo) { public static File zip(File file, String zipTo) {
File target = null; File target = null;
if (file.exists()) { if (file.exists()) {
target = FileUtil.createFile(zipTo); target = FileUtil.createFile(zipTo);
FileOutputStream fos = null; FileOutputStream fos = null;
ZipOutputStream zos = null; ZipOutputStream zos = null;
try { try {
fos = new FileOutputStream(target); fos = new FileOutputStream(target);
zos = new ZipOutputStream(new BufferedOutputStream(fos)); zos = new ZipOutputStream(new BufferedOutputStream(fos));
addEntry(null, file, zos); addEntry(null, file, zos);
} catch (IOException e) { } catch (IOException e) {
throw new ZipUtilException(e); throw new ZipUtilException(e);
} finally { } finally {
try { try {
if (zos != null) { if (zos != null) {
zos.close(); zos.close();
} }
if (fos != null) { if (fos != null) {
fos.close(); fos.close();
} }
} catch (IOException e) { } catch (IOException e) {
throw new ZipUtilException(e); throw new ZipUtilException(e);
} }
} }
} }
return target; return target;
} }
private static void addEntry(String base, File source, ZipOutputStream zos) { private static void addEntry(String base, File source, ZipOutputStream zos) {
String entry = null; String entry = null;
if(StringUtil.isBlank(base)) { if(StringUtil.isBlank(base)) {
entry = source.getName(); entry = source.getName();
} else { } else {
entry = base + File.separator + source.getName(); entry = base + File.separator + source.getName();
} }
if (source.isDirectory()) { if (source.isDirectory()) {
for (File file : source.listFiles()) { for (File file : source.listFiles()) {
addEntry(entry, file, zos); addEntry(entry, file, zos);
} }
} else { } else {
FileInputStream fis = null; FileInputStream fis = null;
BufferedInputStream bis = null; BufferedInputStream bis = null;
try { try {
try { try {
byte[] buffer = new byte[1024 * 10]; byte[] buffer = new byte[1024 * 10];
fis = new FileInputStream(source); fis = new FileInputStream(source);
bis = new BufferedInputStream(fis, buffer.length); bis = new BufferedInputStream(fis, buffer.length);
int read = 0; int read = 0;
zos.putNextEntry(new ZipEntry(entry)); zos.putNextEntry(new ZipEntry(entry));
while ((read = bis.read(buffer, 0, buffer.length)) != -1) { while ((read = bis.read(buffer, 0, buffer.length)) != -1) {
zos.write(buffer, 0, read); zos.write(buffer, 0, read);
} }
zos.closeEntry(); zos.closeEntry();
} catch (IOException e) { } catch (IOException e) {
throw new ZipUtilException(e); throw new ZipUtilException(e);
} finally { } finally {
if (bis != null) { if (bis != null) {
bis.close(); bis.close();
} }
if (fis != null) { if (fis != null) {
fis.close(); fis.close();
} }
} }
} catch (IOException e) { } catch (IOException e) {
throw new ZipUtilException(e); throw new ZipUtilException(e);
} }
} }
} }
public static void unzip(String zipName) { public static void unzip(String zipName) {
File source = new File(zipName); File source = new File(zipName);
unzip(source, source.getParentFile().getAbsolutePath() + source.getName()); unzip(source, source.getParentFile().getAbsolutePath() + source.getName());
} }
public static void unzip(File source) { public static void unzip(File source) {
unzip(source, source.getParentFile().getAbsolutePath() + source.getName()); unzip(source, source.getParentFile().getAbsolutePath() + source.getName());
} }
public static void unzip(String zipName, String targetPath) { public static void unzip(String zipName, String targetPath) {
unzip(new File(zipName), targetPath); unzip(new File(zipName), targetPath);
} }
public static void unzip(File file, String descDir) { public static void unzip(File file, String descDir) {
ZipFile zipFile = null; ZipFile zipFile = null;
ZipInputStream zis = null; ZipInputStream zis = null;
try { try {
zipFile = new ZipFile(file); zipFile = new ZipFile(file);
zis = new ZipInputStream(new FileInputStream(file)); zis = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null; ZipEntry entry = null;
while((entry = zis.getNextEntry()) != null && !entry.isDirectory()) { while((entry = zis.getNextEntry()) != null && !entry.isDirectory()) {
InputStream is = zipFile.getInputStream(entry); InputStream is = zipFile.getInputStream(entry);
FileUtil.streamToFile(is, descDir + File.separator + entry.getName()); FileUtil.streamToFile(is, descDir + File.separator + entry.getName());
} }
} catch (Exception e) { } catch (Exception e) {
throw new ZipUtilException(e); throw new ZipUtilException(e);
} finally { } finally {
try { try {
if(zipFile != null) { if(zipFile != null) {
zipFile.close(); zipFile.close();
} }
if(zis != null) { if(zis != null) {
zis.close(); zis.close();
} }
} catch (Exception e) { } catch (Exception e) {
throw new ZipUtilException(e); throw new ZipUtilException(e);
} }
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
String targetPath = "D:\\Green\\IDC\\50010"; String targetPath = "D:\\data\\erp\\91310114594794273Y";
File file = ZipUtil.zip(targetPath); File file = ZipUtil.zip(targetPath);
ZipUtil.unzip(file, "D:\\IDC"); 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