package cn.com.yusys.yusp.untiy.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
@Slf4j
public class FileOprCommonUtils {
public static String fileZip(List<String> files){
ZipOutputStream zipOutputStream =null;
OutputStream outputStream= null;
try {
outputStream = new FileOutputStream(CommonVarUtils.UntiyCommonFields.FILE_PATH_SAVE+CommonVarUtils.UntiyCommonFields.FILE_CREATE_ZIP_NAME);
zipOutputStream = new ZipOutputStream(outputStream);
for (String file : files) {
File f = new File(file);
compress(f,zipOutputStream,f.getName(),true);
}
zipOutputStream.close();
outputStream.close();
} catch (Exception e) {
log.error("文件处理是失败!",e.getMessage());
}finally {
IOUtils.closeQuietly(zipOutputStream);
}
return "成功";
}
public static String txtFileMake(List<String> datas){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(CommonVarUtils.UntiyCommonFields.FILE_PATH_SAVE+CommonVarUtils.UntiyCommonFields.FILE_CREATE_TXT_NAME);
StringBuffer buffer = new StringBuffer();
for (String data : datas) {
buffer.append(data).append(CommonVarUtils.UntiyCommonFields.FILE_REG_NAME_WRITE);
}
fileOutputStream.write(buffer.toString().getBytes());
fileOutputStream.flush();
fileOutputStream.close();
}catch (Exception e){
log.error("文件处理失败!失败原因:"+e.getMessage());
}
finally {
IOUtils.closeQuietly(fileOutputStream);
}
return null;
}
public static List<String> fileReader(String flie){
FileInputStream fileInputStream = null;
List<String> txtLists = new ArrayList<>();
try {
fileInputStream = new FileInputStream(flie);
InputStreamReader reader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(reader);
String readLine = null;
while ((readLine = bufferedReader.readLine())!=null){
if (StringUtils.isNotBlank(readLine)) {
txtLists.add(readLine);
}
}
bufferedReader.close();
reader.close();
fileInputStream.close();
} catch (Exception e) {
log.error("文件处理失败!原因是:"+e.getMessage());
}
return txtLists;
}
public static String getZipFileName(String zipFileName){
String[] split = zipFileName.split(".");
return split[0];
}
private static void compress(File sourceFile, ZipOutputStream zos, String name,
boolean KeepDirStructure) throws Exception{
byte[] buf = new byte[1024];
if(sourceFile.isFile()){
zos.putNextEntry(new ZipEntry(name));
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourceFile.listFiles();
if(listFiles == null || listFiles.length == 0){
if(KeepDirStructure){
zos.putNextEntry(new ZipEntry(name + "/"));
zos.closeEntry();
}
}else {
for (File file : listFiles) {
if (KeepDirStructure) {
compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
} else {
compress(file, zos, file.getName(),KeepDirStructure);
}
}
}
}
}
public static List<String> fileUnzip(String url){
List<String> zipList = new ArrayList<>();
try {
File file = new File(url);
ZipFile zipFile = new ZipFile(file);
Enumeration<? extends ZipEntry> enumeration = zipFile.entries();
while (enumeration.hasMoreElements()){
ZipEntry zipEntry = enumeration.nextElement();
if (zipEntry.isDirectory()){
file.mkdirs();
}else {
String urlCode = url.substring(0,url.lastIndexOf("/"));
File tarFile = new File(urlCode+"/"+zipEntry.getName());
if (!tarFile.getParentFile().exists()){
tarFile.getParentFile().mkdirs();
}
tarFile.createNewFile();
zipList.add(tarFile.getAbsolutePath());
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(tarFile);
byte[] readByte = new byte[1024];
int len = -1;
while ((len = is.read(readByte))!=-1){
fos.write(readByte,0,len);
}
fos.close();
is.close();
}
}
zipFile.close();
} catch (Exception e) {
log.error("文件处理失败!原因是:"+e.getMessage());
}finally {
}
return zipList;
}
public static String fileZips(List<String> files){
ZipOutputStream zipOutputStream =null;
OutputStream outputStream= null;
try {
outputStream = new FileOutputStream(CommonVarUtils.UntiyCommonFields.FILE_PATH_SAVE+CommonVarUtils.UntiyCommonFields.FILE_CREATE_ZIP_CONTENT_NAME);
zipOutputStream = new ZipOutputStream(outputStream);
StringBuffer buffer = new StringBuffer();
for (String s : files) {
buffer.append(s).append(CommonVarUtils.UntiyCommonFields.FILE_REG_NAME_WRITE);
}
if (buffer.length()<=0){
throw new Exception("待压缩文件大小为空!");
}
zipOutputStream.putNextEntry(new ZipEntry(getZipFileName(CommonVarUtils.UntiyCommonFields.FILE_CREATE_ZIP_CONTENT_NAME)));
zipOutputStream.write(buffer.toString().getBytes(CommonVarUtils.UntiyCommonFields.FILE_ENCODE_TYPE_UTF_8));
zipOutputStream.closeEntry();
zipOutputStream.close();
outputStream.close();
} catch (Exception e) {
log.error("文件处理是失败!",e.getMessage());
}finally {
IOUtils.closeQuietly(zipOutputStream);
}
return "成功";
}
public List<String> getTxtUrlPath(String url){
File file = new File(url);
List<String> txtUrlPath= new ArrayList<>();
if (file.exists()){
File[] files = file.listFiles();
if (files.length==0){
return txtUrlPath;
}else {
for (File file1 : files) {
if (file1.isDirectory()){
getTxtUrlPath(file1.getAbsolutePath());
}else {
txtUrlPath.add(file1.getAbsolutePath());
}
}
}
}
return txtUrlPath;
}
public static List<String> fileUnzipss2(String data){
ZipInputStream zipInputStream = null;
List<String> zipList = new ArrayList<>();
try {
File file = new File(CommonVarUtils.UntiyCommonFields.FILE_PATH_SAVE+CommonVarUtils.UntiyCommonFields.FILE_CREATE_ZIP_NAME);
FileInputStream fis = new FileInputStream(file);
zipInputStream = new ZipInputStream(fis);
ZipEntry zipEntry = null;
File unZip = null;
while((zipEntry = zipInputStream.getNextEntry())!=null){
if (!zipEntry.isDirectory()){
unZip = new File(CommonVarUtils.UntiyCommonFields.FILE_PATH_SAVE+"/"+zipEntry.getName());
if (!unZip.getParentFile().exists()){
unZip.getParentFile().mkdirs();
}
unZip.createNewFile();
OutputStream os = new FileOutputStream(unZip);
BufferedOutputStream bos = new BufferedOutputStream(os);
byte[] readByte = new byte[1024];
int len = -1;
while ((len = zipInputStream.read(readByte))!=-1){
bos.write(readByte,0,len);
}
bos.close();
os.close();
}else {
file.mkdirs();
}
}
zipInputStream.close();
fis.close();
} catch (Exception e) {
log.error("文件处理失败!原因是:"+e.getMessage());
}finally {
IOUtils.closeQuietly(zipInputStream);
}
return null;
}
}
|