java 文件操作
1、判断压缩包解压后的大小
/**
* 读取解压缩文件的大小
* @param path 文件路径
* @return 文件大小 单位byte
*/
public static long getUnZipFileSize(String path) {
long size = 0l;
try {
InputStream fis = new FileInputStream(new File(path));
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
size += entry.getSize();
}
}
zis.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(size > 0)
return size / (1024 * 1024);
return size;
}
2、解压缩文件
/**
* 解压缩文件
* @param path 文件路径
* @param targetPath 目标路径
* @throws IOException
*/
public static void disZIP(String path, String targetPath)
throws IOException {
try {
// Create a ZipInputStream to read the zip file
BufferedOutputStream dest = null;
// InputStream fis = getResources().openRawResource(R.raw.files);
InputStream fis = new FileInputStream(new File(path));
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(fis));
// Loop over all of the entries in the zip file
int count;
byte data[] = new byte[1024];
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
String destination = targetPath;
File file = new File(destination);
if (!file.exists())
file.mkdirs();
String destFN = destination + File.separator
+ entry.getName();
// Write the file to the file system
FileOutputStream fos = new FileOutputStream(destFN);
dest = new BufferedOutputStream(fos, 1024);
while ((count = zis.read(data, 0, 1024)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
} else {
File f = new File(targetPath + File.separator
+ entry.getName());
f.mkdirs();
}
}
zis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3、拷贝目录
/**
* 拷贝目录
* @param sourceDir
* @param targetDir
* @throws IOException
*/
public static void copyFolder(String sourceDir, String targetDir) throws IOException {
// 新建目标目录
(new File(targetDir)).mkdirs();
// 获取源文件夹当前下的文件或目录
File[] file = (new File(sourceDir)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
// 源文件
File sourceFile = file[i];
// 目标文件
File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName());
copyFile(sourceFile, targetFile);
}
if (file[i].isDirectory()) {
// 准备复制的源文件夹
String dir1 = sourceDir + "/" + file[i].getName();
// 准备复制的目标文件夹
String dir2 = targetDir + "/" + file[i].getName();
copyFolder(dir1, dir2);
}
}
}
4、删除目录
/**
* 删除目录(文件夹)以及目录下的文件
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean 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;
// 删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
/**
* 删除单个文件
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}

浙公网安备 33010602011771号