Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CityDoctor
CityDoctor2
Commits
89db7652
Commit
89db7652
authored
Jan 15, 2025
by
Riegel
Browse files
Refactor: Rework zipping algorithm
parent
be520dc3
Pipeline
#10903
passed with stage
in 5 minutes and 8 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
CityDoctorParent/CityDoctorModel/src/main/java/de/hft/stuttgart/citydoctor2/utils/ArchivePacker.java
View file @
89db7652
...
...
@@ -4,15 +4,14 @@ import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import
de.hft.stuttgart.citydoctor2.zip.CityGmlZipArchive
;
import
de.hft.stuttgart.citydoctor2.zip.CityGmlZipEntry
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.FilenameUtils
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
import
java.io.*
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
...
...
@@ -23,9 +22,9 @@ public class ArchivePacker {
private
ArchivePacker
(){}
public
static
void
packArchive
(
String
targetPath
,
CityGmlZipArchive
archive
){
if
(!
targetPath
.
endsWith
(
".zip"
)){
targetPath
=
targetPath
.
concat
(
".zip"
);
}
Path
outputPath
=
Path
.
of
(
targetPath
);
outputPath
=
outputPath
.
getParent
().
resolve
(
FilenameUtils
.
removeExtension
(
outputPath
.
getFileName
().
toString
())
+
".zip"
);
Path
tmpDir
=
null
;
try
{
tmpDir
=
Files
.
createTempDirectory
(
"zipTmp"
);
...
...
@@ -35,10 +34,10 @@ public class ArchivePacker {
continue
;
}
CityDoctorModel
model
=
entry
.
getModel
();
String
file
name
=
tmpDir
.
toString
()
+
File
.
separator
+
entry
.
getFullFileName
();
model
.
saveAs
(
file
name
,
model
.
isValidated
());
Path
file
Path
=
tmpDir
.
resolve
(
entry
.
getFullFileName
()
)
;
model
.
saveAs
(
file
Path
.
toString
()
,
model
.
isValidated
());
}
zip
Directory
(
targetPath
,
tmpDir
.
toString
());
zip
Folder
(
tmpDir
.
toFile
(),
outputPath
.
toFile
());
}
catch
(
Exception
e
){
logger
.
error
(
e
);
...
...
@@ -53,45 +52,46 @@ public class ArchivePacker {
}
}
private
static
void
zipDirectory
(
String
targetPath
,
String
sourcePath
){
File
directory
=
new
File
(
sourcePath
);
if
(!
directory
.
exists
()){
throw
new
IllegalStateException
(
"Directory "
+
sourcePath
+
" does not exist"
);
public
static
void
zipFolder
(
File
folder
,
File
zipFile
)
throws
IOException
{
zipFolder
(
folder
,
new
FileOutputStream
(
zipFile
));
}
public
static
void
zipFolder
(
File
folder
,
OutputStream
outputStream
)
throws
IOException
{
try
(
ZipOutputStream
zipOutputStream
=
new
ZipOutputStream
(
outputStream
))
{
processFolder
(
folder
,
zipOutputStream
,
folder
.
getPath
().
length
()
+
1
);
}
List
<
String
>
fileList
=
new
ArrayList
<>(
Arrays
.
asList
(
directory
.
list
()));
byte
[]
buffer
=
new
byte
[
1024
];
}
try
(
FileOutputStream
fos
=
new
FileOutputStream
(
targetPath
);
ZipOutputStream
zos
=
new
ZipOutputStream
(
fos
)){
for
(
String
file
:
fileList
){
ZipEntry
ze
=
new
ZipEntry
(
file
);
zos
.
putNextEntry
(
ze
);
Path
sourceFile
=
Path
.
of
(
sourcePath
+
File
.
separator
+
file
);
try
(
FileInputStream
in
=
new
FileInputStream
(
sourceFile
.
toAbsolutePath
().
toString
())){
int
len
;
while
((
len
=
in
.
read
(
buffer
))
>
0
)
{
zos
.
write
(
buffer
,
0
,
len
);
}
private
static
void
processFolder
(
File
folder
,
ZipOutputStream
zipOutputStream
,
int
prefixLength
)
throws
IOException
{
for
(
File
file
:
folder
.
listFiles
())
{
if
(
file
.
isFile
())
{
ZipEntry
zipEntry
=
new
ZipEntry
(
file
.
getPath
().
substring
(
prefixLength
));
zipOutputStream
.
putNextEntry
(
zipEntry
);
try
(
FileInputStream
inputStream
=
new
FileInputStream
(
file
))
{
IOUtils
.
copy
(
inputStream
,
zipOutputStream
);
}
zipOutputStream
.
closeEntry
();
}
else
if
(
file
.
isDirectory
())
{
processFolder
(
file
,
zipOutputStream
,
prefixLength
);
}
logger
.
info
(
"Successfully created zip-archive"
);
}
catch
(
IOException
e
)
{
logger
.
error
(
e
);
}
}
public
static
void
packAndDeleteDirectory
(
String
directoryPath
)
throws
IOException
{
Path
p
ath
=
Path
.
of
(
directoryPath
);
if
(!
path
.
to
File
()
.
exists
())
{
Path
sourceP
ath
=
Path
.
of
(
directoryPath
);
if
(!
File
s
.
exists
(
sourcePath
))
{
throw
new
IllegalStateException
(
"Directory "
+
directoryPath
+
" does not exist"
);
}
if
(!
path
.
toFile
().
isDirectory
())
{
if
(!
Files
.
isDirectory
(
sourcePath
))
{
throw
new
IllegalStateException
(
"Path "
+
directoryPath
+
" is not a directory"
);
}
String
outputPath
=
p
ath
.
getParent
().
resolve
(
p
ath
.
getFileName
()
+
".zip"
)
.
toString
()
;
zip
Directory
(
outputPath
,
directoryPath
);
Path
outputPath
=
sourceP
ath
.
getParent
().
resolve
(
sourceP
ath
.
getFileName
()
+
".zip"
);
zip
Folder
(
sourcePath
.
toFile
(),
outputPath
.
toFile
()
);
FileUtils
.
deleteDirectory
(
p
ath
.
toFile
());
FileUtils
.
deleteDirectory
(
sourceP
ath
.
toFile
());
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment