您的位置:首页 > 编程语言 > Java开发

java中复制文件和文件夹Demo,压缩单个文件夹Demo,压缩文件和文件夹Demo

2018-01-10 15:15 477 查看
public void compressFoldersAndFiles(List<String> filesAndFoldersList, String tempfolder, String filename)
throws Exception {
Calendar rightNow = Calendar.getInstance();
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd_hhmmss");
String folder = "C:\\" + fmt.format(rightNow.getTime()); // 临时存储文件夹
if (!filesAndFoldersList.isEmpty()) {
for (int i = 0; i < filesAndFoldersList.size(); i++) {
File file = new File(filesAndFoldersList.get(i));
if (file.isDirectory()) {
String temp = folder + filesAndFoldersList.get(i).substring(2); // 去掉c盘盘符,保留下载路径
if (!copyFolder(filesAndFoldersList.get(i), temp)) {
throw new Exception("filesAndFoldersList copyFolder error");
}
} else if (file.isFile()) {
String temp = folder + filesAndFoldersList.get(i).substring(2); // 去掉c盘盘符
int b = temp.lastIndexOf("\\");
String tempFolder = temp.substring(0, b);
File Tempfile = new File(tempFolder);
Tempfile.mkdirs();
if (!copyFile(filesAndFoldersList.get(i), temp)) {
throw new Exception("filesAndFoldersList copyFile error");
}
} else {
throw new Exception("filesAndFoldersList error");
}
}
compressSingleFolder(folder, tempfolder, filename);
File file = new File(folder);
while (file.exists()) { // 删除临时文件夹
System.gc();
deleteDir(file);
}
} else {
throw new Exception("filesAndFoldersList is empty");
}
}



private static boolean copyFile(String oldPathAndFileName, String targetPathAndFileName) {
boolean flag = false;
try {
int byteread = 0;
File oldfile = new File(oldPathAndFileName);
if (oldfile.exists()) {
InputStream inStream = new FileInputStream(oldPathAndFileName);
FileOutputStream fs = new FileOutputStream(targetPathAndFileName);
byte[] buffer = new byte[1444];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
flag = true;
return flag;
} else {
flag = false;
return flag;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}

private static boolean copyFolder(String oldPath, String newPath) {
boolean flag = false;
try {
File newFile = new File(newPath);
newFile.setExecutable(false);
newFile.setReadable(true, true);
newFile.setWritable(true, true);
newFile.mkdirs();
File a = new File(oldPath);
if (a.exists()) {
a.setExecutable(false);
a.setReadable(true, true);
a.setWritable(true, true);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(
newPath + File.separator + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {
copyFolder(oldPath + File.separator + file[i], newPath + File.separator + file[i]);
}
}
flag = true;
return flag;
} else {
flag = false;
return flag;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
private static List<String> listFile = new ArrayList<String>();

public void compressSingleFolder(String oldPathAndFolderName, String tempfolder, String filename) throws Exception {
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;
if (getFile(oldPathAndFolderName)) {
try {
File zipFile = new File(tempfolder + "\\" + filename + ".zip");
if (zipFile.exists()) {
throw new Exception(tempfolder + " has already exists " + filename + ".zip");
} else {
if (!zipFile.exists()) {
zipFile.getParentFile().mkdirs();
}
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(new BufferedOutputStream(fos));
byte[] bufs = new byte[1024 * 1024];
for (int i = 0; i < listFile.size(); i++) {
try {
ZipEntry zipEntry = new ZipEntry(listFile.get(i));
zos.putNextEntry(zipEntry);
fis = new FileInputStream(listFile.get(i));
bis = new BufferedInputStream(fis, 1024 * 1024);
int read = 0;
while ((read = bis.read(bufs, 0, 1024 * 1024)) != -1) {
zos.write(bufs, 0, read);
}
} catch (Exception e) {
throw new Exception("downloadSingleFolder ZipEntry error");
}
}
listFile.clear();
}
} catch (FileNotFoundException | JSONException e) {
e.printStackTrace();
throw new RuntimeException(
b26f
e);
} finally {
try {
if (null != bis)
bis.close();
if (null != zos)
zos.close();
if (null != fos)
fos.close();
if (null != fis)
fis.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
} else {
throw new Exception("folder not exists or folder is blank");
}
}

private static boolean getFile(String path) {
File file = new File(path);
if (file.exists()) {
File[] tempList = file.listFiles();
for (File f : tempList) {
if (f.isFile()) {
listFile.add(f.getPath());
continue;
}
if (f.isDirectory()) {
getFile(f.getPath());
}
}
return true;
} else {
return false;
}
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: