Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
CityDoctor
CityDoctor2
Commits
283e0d57
Commit
283e0d57
authored
3 months ago
by
Riegel
Browse files
Options
Download
Email Patches
Plain Diff
Feat: Add exporting of CityGmlArchive to zip-file
parent
4c68f544
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CityDoctorParent/Extensions/CityDoctorZipLoader/src/main/java/de/hft/stuttgart/citydoctor2/ziploader/CityGmlArchive.java
+3
-8
...e/hft/stuttgart/citydoctor2/ziploader/CityGmlArchive.java
CityDoctorParent/Extensions/CityDoctorZipLoader/src/main/java/de/hft/stuttgart/citydoctor2/ziploader/utils/ArchivePacker.java
+98
-0
.../stuttgart/citydoctor2/ziploader/utils/ArchivePacker.java
CityDoctorParent/Extensions/CityDoctorZipLoader/src/test/java/de/hft/stuttgart/citydoctor2/ziploader/ZipTest.java
+5
-0
.../java/de/hft/stuttgart/citydoctor2/ziploader/ZipTest.java
with
106 additions
and
8 deletions
+106
-8
CityDoctorParent/Extensions/CityDoctorZipLoader/src/main/java/de/hft/stuttgart/citydoctor2/ziploader/CityGmlArchive.java
+
3
-
8
View file @
283e0d57
package
de.hft.stuttgart.citydoctor2.ziploader
;
import
de.hft.stuttgart.citydoctor2.check.Checker
;
import
de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel
;
import
de.hft.stuttgart.citydoctor2.parser.ParserConfiguration
;
import
de.hft.stuttgart.citydoctor2.ziploader.utils.ArchivePacker
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.logging.log4j.LogManager
;
import
org.apache.logging.log4j.Logger
;
...
...
@@ -68,14 +70,7 @@ public class CityGmlArchive {
}
public
void
exportToZipFile
(
String
path
)
{
if
(!
path
.
endsWith
(
".zip"
)){
throw
new
IllegalArgumentException
(
"Target zip-filepath must end with '.zip'"
);
}
if
(
validated
)
{
//TODO: Export of validation reports for each entry
}
ArchivePacker
.
packArchive
(
path
,
this
);
}
public
void
checkEntries
(){
...
...
This diff is collapsed.
Click to expand it.
CityDoctorParent/Extensions/CityDoctorZipLoader/src/main/java/de/hft/stuttgart/citydoctor2/ziploader/utils/ArchivePacker.java
0 → 100644
+
98
-
0
View file @
283e0d57
package
de.hft.stuttgart.citydoctor2.ziploader.utils
;
import
de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel
;
import
de.hft.stuttgart.citydoctor2.ziploader.CityGmlArchive
;
import
de.hft.stuttgart.citydoctor2.ziploader.CityGmlZipEntry
;
import
org.apache.commons.io.FileUtils
;
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.List
;
import
java.util.zip.ZipEntry
;
import
java.util.zip.ZipOutputStream
;
public
class
ArchivePacker
{
private
static
final
Logger
logger
=
LogManager
.
getLogger
(
ArchivePacker
.
class
);
private
ArchivePacker
(){}
public
static
void
packArchive
(
String
targetPath
,
CityGmlArchive
archive
){
if
(!
targetPath
.
endsWith
(
".zip"
)){
throw
new
IllegalArgumentException
(
"Target zip-filepath must end with '.zip'"
);
}
Path
tmpDir
=
null
;
try
{
tmpDir
=
Files
.
createTempDirectory
(
"zipTmp"
);
tmpDir
.
toFile
().
deleteOnExit
();
for
(
CityGmlZipEntry
entry
:
archive
.
getEntries
())
{
if
(
entry
.
isErroneousEntry
()){
continue
;
}
CityDoctorModel
model
=
entry
.
getModel
();
String
filename
=
tmpDir
.
toString
()
+
"\\"
+
entry
.
getFileName
();
model
.
saveAs
(
filename
,
entry
.
isValidated
());
}
zipDirectory
(
targetPath
,
tmpDir
.
toString
());
}
catch
(
Exception
e
){
logger
.
error
(
e
);
}
finally
{
if
(
tmpDir
!=
null
)
{
try
{
FileUtils
.
deleteDirectory
(
tmpDir
.
toFile
());
}
catch
(
IOException
e
)
{
logger
.
error
(
e
);
}
}
}
}
private
static
void
zipDirectory
(
String
targetPath
,
String
sourcePath
){
List
<
String
>
fileList
=
new
ArrayList
<>();
File
directory
=
new
File
(
sourcePath
);
for
(
String
file
:
directory
.
list
()){
fileList
.
add
(
file
);
}
byte
[]
buffer
=
new
byte
[
1024
];
String
source
=
new
File
(
sourcePath
).
getName
();
FileOutputStream
fos
=
null
;
ZipOutputStream
zos
=
null
;
try
{
fos
=
new
FileOutputStream
(
targetPath
);
zos
=
new
ZipOutputStream
(
fos
);
FileInputStream
in
=
null
;
for
(
String
file
:
fileList
){
ZipEntry
ze
=
new
ZipEntry
(
file
);
zos
.
putNextEntry
(
ze
);
try
{
in
=
new
FileInputStream
(
sourcePath
+
"\\"
+
file
);
int
len
;
while
((
len
=
in
.
read
(
buffer
))
>
0
)
{
zos
.
write
(
buffer
,
0
,
len
);
}
}
finally
{
if
(
in
!=
null
)
{
in
.
close
();
}
}
}
zos
.
closeEntry
();
logger
.
info
(
"Successfully created zip-archive"
);
}
catch
(
IOException
e
)
{
logger
.
error
(
e
);
}
finally
{
try
{
zos
.
close
();
}
catch
(
IOException
e
)
{
logger
.
error
(
e
);
}
}
}
}
This diff is collapsed.
Click to expand it.
CityDoctorParent/Extensions/CityDoctorZipLoader/src/test/java/de/hft/stuttgart/citydoctor2/ziploader/ZipTest.java
+
5
-
0
View file @
283e0d57
...
...
@@ -69,6 +69,11 @@ public class ZipTest {
CityGmlArchive
cgmlExport
=
CityGmlArchive
.
fromZipFile
(
expPath
,
config
);
assertNotNull
(
cgmlExport
);
assertEquals
(
5
,
cgmlExport
.
getEntries
().
size
());
for
(
CityGmlZipEntry
entry
:
cgmlExport
.
getEntries
())
{
assertNotNull
(
entry
);
assertTrue
(
entry
.
getFileName
().
matches
(
"^mock[1-5].gml$"
));
assertNotNull
(
entry
.
getModel
());
}
}
catch
(
Exception
e
)
{
// Rethrow Exceptions to ensure deletion of tmpDir with finally block
throw
e
;
...
...
This diff is collapsed.
Click to expand it.
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
Menu
Explore
Projects
Groups
Snippets