|
@@ -39,8 +39,9 @@ public class FileManager {
|
|
|
}
|
|
|
} catch (IOException e) {}finally {
|
|
|
try {
|
|
|
- if (Objects.nonNull(stream))
|
|
|
+ if (Objects.nonNull(stream)){
|
|
|
stream.close();
|
|
|
+ }
|
|
|
} catch (IOException e) {}
|
|
|
}
|
|
|
if ( size > 0 ) {
|
|
@@ -392,7 +393,7 @@ public class FileManager {
|
|
|
FileInputStream fileOutputStream = null;
|
|
|
try {
|
|
|
fileOutputStream = new FileInputStream(filePath);
|
|
|
- } catch (FileNotFoundException e) {}
|
|
|
+ } catch (FileNotFoundException ignored) {}
|
|
|
return fileOutputStream;
|
|
|
}
|
|
|
|
|
@@ -441,51 +442,55 @@ public class FileManager {
|
|
|
try {
|
|
|
fileOutputStream = new FileOutputStream(tempZipFilePath);
|
|
|
zipOutputStream = new ZipOutputStream(fileOutputStream);
|
|
|
- compressFolder(folderToCompress, zipOutputStream);
|
|
|
+ compressFolder(folderToCompress, zipOutputStream, "");
|
|
|
} catch (IOException e) {
|
|
|
result = false;
|
|
|
} finally {
|
|
|
try {
|
|
|
- if ( Objects.nonNull(fileOutputStream) ) {
|
|
|
- fileOutputStream.close();
|
|
|
- }
|
|
|
if ( Objects.nonNull(zipOutputStream)) {
|
|
|
+ zipOutputStream.finish();
|
|
|
+ zipOutputStream.flush();
|
|
|
zipOutputStream.close();
|
|
|
}
|
|
|
+ if ( Objects.nonNull(fileOutputStream) ) {
|
|
|
+ fileOutputStream.close();
|
|
|
+ }
|
|
|
} catch (Exception ignored) {}
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- private void compressFolder(String sourceFolder, ZipOutputStream zipOutputStream) {
|
|
|
- System.out.println( "sourceFolder" );
|
|
|
- System.out.println( sourceFolder );
|
|
|
+ private void compressFolder(String sourceFolder, ZipOutputStream zipOutputStream, String folderLevel) {
|
|
|
File folder = new File(sourceFolder);
|
|
|
File[] files = folder.listFiles();
|
|
|
if (files != null) {
|
|
|
for (File file : files) {
|
|
|
+ String folderLevelZipEntry = file.getName();
|
|
|
+ if ( !"".equals(folderLevel) ) {
|
|
|
+ folderLevelZipEntry = folderLevel + File.separator + file.getName();
|
|
|
+ }
|
|
|
if (file.isDirectory()) {
|
|
|
- compressFolder( sourceFolder + separator + file.getName(), zipOutputStream);
|
|
|
- } else {
|
|
|
- addToZipFile(file.getName(), sourceFolder + separator + file.getName(), zipOutputStream);
|
|
|
+ compressFolder( file.getAbsolutePath(), zipOutputStream, folderLevelZipEntry );
|
|
|
+ } else if (file.isFile()) {
|
|
|
+ addToZipFile(file.getAbsolutePath(), zipOutputStream, folderLevelZipEntry);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void addToZipFile(String fileName, String fileAbsolutePath, ZipOutputStream zipOutputStream) {
|
|
|
- System.out.println( "fileAbsolutePath" );
|
|
|
- System.out.println( fileAbsolutePath );
|
|
|
+ private void addToZipFile(String fileAbsolutePath, ZipOutputStream zipOutputStream, String folderLevel) {
|
|
|
FileInputStream fileInputStream = null;
|
|
|
- ZipEntry entry = new ZipEntry(fileName);
|
|
|
+ ZipEntry entry = new ZipEntry(folderLevel);
|
|
|
try {
|
|
|
- zipOutputStream.putNextEntry(entry);
|
|
|
fileInputStream = new FileInputStream(fileAbsolutePath);
|
|
|
- byte[] buffer = new byte[1024];
|
|
|
+ zipOutputStream.putNextEntry(entry);
|
|
|
+ zipOutputStream.setComment("爱扣钉 档案系统");
|
|
|
+ byte[] buffer = new byte[4096];
|
|
|
int bytesRead;
|
|
|
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
|
|
|
zipOutputStream.write(buffer, 0, bytesRead);
|
|
|
}
|
|
|
+ zipOutputStream.closeEntry();
|
|
|
} catch (Exception ignored) {}finally {
|
|
|
try {
|
|
|
if ( Objects.nonNull(fileInputStream)) {
|