Java 文件操作大集合

package com.sess.hny.sys.fileserver;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public final class FileOperateUtils {

 文件夹判断,当没有时创建
public static void directoryExistsAndCreate(String filePath) throws Exception
{
File file = new File(filePath);
if( !file.exists() )
{
if (!file.getParentFile().exists()) {// 判断目标文件所在的目录是否存在
if (!file.getParentFile().mkdirs()) {// 判断创建目录是否成功
throw new Exception("创建文件路径失败!");
}}}
}

判断文件及文件夹是否存在,存在就删除
public static void directoryExistsAndRemove(String filePath) throws Exception
{
File file = new File(filePath);
if( file.exists() )
{
if( file.isDirectory() )
{
for( File fl : file.listFiles() )
{
directoryExistsAndRemove(fl);
}
file.delete();
}
else
{
file.delete();
}
}
}

拷贝文件
public static void copyFile(String srcPath, String dstPath) throws Exception
{
if( null != srcPath && srcPath.trim().length() > 0 && null != dstPath && dstPath.trim().length() > 0)
{
if( !srcPath.equals(dstPath) )
{
if( directoryExists(srcPath) )
{
if( directoryExists(dstPath) ) throw new Exception("目标文件已经存在");
else
{
BufferedInputStream srcBuff = null;
BufferedOutputStream dstBuff = null;
try
{
//源文件流
FileInputStream src = new FileInputStream(srcPath);
srcBuff = new BufferedInputStream(src);
//目标文件流
FileOutputStream dst = new FileOutputStream(dstPath);
dstBuff = new BufferedOutputStream(dst);
int len = 0;
while( (len = srcBuff.read()) != -1 )
{
dstBuff.write(len);
}
}
catch (Exception e)
{
throw e;
}
finally
{
if( null != srcBuff) srcBuff.close();
if( null != dstBuff) dstBuff.close();
}
}
}
else
{
throw new Exception("源目录文件不存在");
}
}
}
}

 压缩zip文件
public static String fileToZip(String sourceFilePath, String zipParentName, boolean verifyExists, IFileServerConfig fileConfig) throws Exception
{
if( !directoryExists(sourceFilePath) ) throw new Exception("压缩文件目录:" + sourceFilePath + "文件或文件夹不存在!!!");
String zipFilePath = null;
String fileName = null;
int sourceIndex1 = sourceFilePath.lastIndexOf("/");
int sourceIndex2 = sourceFilePath.lastIndexOf("\\");
if( sourceFilePath.endsWith("/") )
{
sourceFilePath = sourceFilePath.substring(0, sourceIndex1);
}
else if( sourceFilePath.endsWith("\\") )
{
sourceFilePath = sourceFilePath.substring(0, sourceIndex2);
}
// 获取文件夹压缩名
if( sourceIndex1 != -1 && sourceIndex1 > sourceIndex2 )
{
zipFilePath = sourceFilePath.substring(0, sourceIndex1);
fileName = sourceFilePath.substring(sourceIndex1 + 1);
}
else if( sourceIndex2 != -1 && sourceIndex1 < sourceIndex2 )
{
zipFilePath = sourceFilePath.substring(0, sourceIndex2);
fileName = sourceFilePath.substring(sourceIndex2 + 1);
}
else throw new Exception("文件目录不正确!!!");
if( null == zipParentName || zipParentName.trim().length() == 0) zipParentName = fileName;
return fileToZip(sourceFilePath, zipFilePath, fileName, zipParentName, verifyExists, fileConfig);
}

// 文件压缩成zip
private static String fileToZip(String sourceFilePath, String zipFilePath, String fileName, String parentName, boolean verifyExists, IFileServerConfig fileConfig) throws Exception
{
File sourceFile = new File(sourceFilePath);
String filePath = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
try
{
File zipFile = new File(zipFilePath + "/" + fileName + ".zip");
//文件夹是否存在处理
if( zipFile.exists() )
{
if( verifyExists ) throw new Exception(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件.");
else zipFile.delete();
}
filePath = zipFile.getPath();
File[] sourceFiles = sourceFile.listFiles();
if( null == sourceFiles || sourceFiles.length < 0 ) throw new Exception("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩.");
fos = new FileOutputStream(zipFile);
zos= new ZipOutputStream( new BufferedOutputStream(fos) );
for(int i = 0; i < sourceFiles.length; i++)
{
addZipFile(sourceFiles[i], parentName, zos, fileConfig);
}
return filePath;
}
catch (Exception e)
{
throw e;
}
finally
{
try
{
if( null != zos ) zos.close();
}
catch (Exception e2)
{
throw e2;
}
}
}

// 添加压缩文件目录
private static void addZipFile(File file, String parentPath, ZipOutputStream zos, IFileServerConfig fileConfig) throws Exception
{
if( null == file ) throw new Exception("file不能为空!!!");
if( null == zos ) throw new Exception("zos不能为空!!!");
FileInputStream fis = null;
BufferedInputStream bis = null;
int len = 1024 * 10;
byte[] bufs = new byte[len];
String fileName = null;
int read = 0;
try
{
if( file.isDirectory() )
{
parentPath += "/" + file.getName() + "/";
// 读取待压缩的文件并写进压缩包里
File[] files = file.listFiles();
if( null != files && files.length > 0 )
{
for(int i = 0; i < files.length; i++)
{
addZipFile(files[i], parentPath, zos, fileConfig);
}
}
else
{
ZipEntry zipEntry = new ZipEntry(parentPath);
zos.putNextEntry(zipEntry);
}
}
else
{
// 创建ZIP实体,并添加进压缩包
if( fileConfig == null ) fileName = file.getName();
else fileName = fileConfig.getZipEntry(file);
ZipEntry zipEntry = new ZipEntry(parentPath + "/" + fileName);
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis, len);
read = 0;
while( ( read = bis.read(bufs, 0, len) ) != -1)
{
zos.write(bufs, 0, read);
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
try
{
if( null != bis ) bis.close();
}
catch (Exception e2)
{
throw e2;
}
}
}

public static void main(String[] args)
{
try
{
FileOperateUtils.fileToZip("F:\\lmm\\learn", null, false, null);
System.out.println("打包完成");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

posted on 2017-12-07 14:54  小麦333  阅读(442)  评论(0编辑  收藏  举报

导航