private static final int BUFFER = 1024 * 10;
/**
* copy file1 to folder file2
* @param file1
* @param file2
*/
public static void copy(String file1, String file2) {
File src = new File(file1);
File dst = new File(file2);
if (!dst.exists()) {
dst.mkdirs();
}
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), 16 * 1024);
FileOutputStream f = new FileOutputStream(dst + file1.substring(file1.lastIndexOf("/"))); //一定要加上文件名称
out = new BufferedOutputStream(f, 16 * 1024);
byte[] buffer = new byte[16 * 1024];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* pack file folder to zip
* @param sourceFilePaths source files path
* @param zipFilePath dest zip path
* @param fileName zip file name
* @return
*/
public static boolean fileToZip(String[] sourceFilePaths, String zipFilePath, String fileName){
boolean flag = false;
File zipFile = new File(zipFilePath + "/" + fileName +".zip");
if(zipFile.exists()){
Log.d(TAG, zipFilePath + " The file already exists in the directory " + fileName +".zip");
delete(zipFilePath + "/" + fileName +".zip");
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream(zipFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
File zipFolder = new File(zipFilePath);
if (!zipFolder.exists()) {
zipFolder.mkdirs();
}
for (String sourceFilePath : sourceFilePaths) {
File sourceFile = new File(sourceFilePath);
if(!sourceFile.exists()){
Log.d(TAG, "The file to be compressed does not exist " + sourceFilePath);
}else{
flag = compress(sourceFile, zos);
if (!flag) {
return false;
}
}
}
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
/**
* compress file and folder
* @param file file to compress
* @param out
* @return
*/
public static boolean compress(File file, ZipOutputStream out) {
boolean flag = false;
if (file.isFile()) {
if (!file.exists()) {
return false;
}
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(file.getName());
out.putNextEntry(entry);
int count;
byte data[] = new byte[BUFFER];
while ((count = bis.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
flag = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bis != null) bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
File[] files = file.listFiles();
if (files != null && files.length != 0){
for (int i = 0; i < files.length; i++) {
if (!compress(files[i], out)) {
return false;
}
}
}
flag = true;
}
return flag;
}
/**
* clear file folder
* @param path
*/
public static void delete(String path) {
File file = new File(path);
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for(File temp : files){
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delete(temp.getAbsolutePath());
temp.delete();
}
}
}
}
android文件复制、清空文件夹、压缩文件
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 注意:本文原创,转载请注明出处。欢迎关注我的 简书 。 有个需求,需要压缩文件夹。文件夹下有子文件夹以及文件。我...
- 黑色的海岛上悬着一轮又大又圆的明月,毫不嫌弃地把温柔的月色照在这寸草不生的小岛上。一个少年白衣白发,悠闲自如地倚坐...