Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
openbis
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
sispub
openbis
Commits
24664323
Commit
24664323
authored
10 years ago
by
jakubs
Browse files
Options
Downloads
Patches
Plain Diff
SSDM-1081 multidataset archiver
SVN: 32707
parent
72aa92c7
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
common/source/java/ch/systemsx/cisd/common/filesystem/tar/Untar.java
+84
-37
84 additions, 37 deletions
...ce/java/ch/systemsx/cisd/common/filesystem/tar/Untar.java
with
84 additions
and
37 deletions
common/source/java/ch/systemsx/cisd/common/filesystem/tar/Untar.java
+
84
−
37
View file @
24664323
...
@@ -25,6 +25,7 @@ import java.io.IOException;
...
@@ -25,6 +25,7 @@ import java.io.IOException;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.ListIterator
;
import
java.util.ListIterator
;
import
java.util.Map
;
import
org.apache.commons.compress.archivers.tar.TarArchiveEntry
;
import
org.apache.commons.compress.archivers.tar.TarArchiveEntry
;
import
org.apache.commons.compress.archivers.tar.TarArchiveInputStream
;
import
org.apache.commons.compress.archivers.tar.TarArchiveInputStream
;
...
@@ -41,8 +42,6 @@ public class Untar implements Closeable
...
@@ -41,8 +42,6 @@ public class Untar implements Closeable
private
final
static
int
DEFAULT_BUFFER_SIZE
=
128
*
1024
;
private
final
static
int
DEFAULT_BUFFER_SIZE
=
128
*
1024
;
private
final
List
<
TarArchiveEntry
>
dirEntries
=
new
ArrayList
<
TarArchiveEntry
>();
private
final
TarArchiveInputStream
in
;
private
final
TarArchiveInputStream
in
;
private
final
byte
[]
buf
;
private
final
byte
[]
buf
;
...
@@ -69,46 +68,43 @@ public class Untar implements Closeable
...
@@ -69,46 +68,43 @@ public class Untar implements Closeable
*/
*/
public
void
extract
(
final
File
rootDirectory
)
throws
IOException
public
void
extract
(
final
File
rootDirectory
)
throws
IOException
{
{
final
List
<
TarArchiveEntry
>
dirEntries
=
new
ArrayList
<
TarArchiveEntry
>();
TarArchiveEntry
entry
;
TarArchiveEntry
entry
;
while
((
entry
=
in
.
getNextTarEntry
())
!=
null
)
while
((
entry
=
in
.
getNextTarEntry
())
!=
null
)
{
{
final
File
entryFile
=
new
File
(
rootDirectory
,
entry
.
getName
());
final
File
entryFile
=
new
File
(
rootDirectory
,
entry
.
getName
());
if
(
entry
.
isDirectory
())
extractEntry
(
entry
,
entryFile
,
dirEntries
);
{
}
dirEntries
.
add
(
entry
);
// Set directory metadata in reverse order, i.e. descendants first
entryFile
.
mkdirs
();
final
ListIterator
<
TarArchiveEntry
>
it
=
dirEntries
}
else
if
(
entry
.
isLink
())
.
listIterator
(
dirEntries
.
size
());
{
while
(
it
.
hasPrevious
())
if
(
Unix
.
isOperational
())
{
{
entry
=
it
.
previous
();
Unix
.
createHardLink
(
entry
.
getLinkName
(),
setFileMetadata
(
new
File
(
rootDirectory
,
entry
.
getName
()),
entry
);
entryFile
.
getPath
());
}
}
}
}
else
if
(
entry
.
isSymbolicLink
())
{
/**
if
(
Unix
.
isOperational
())
* Extracts the tar file. Each top level directory is extracted to a different location specified by the {@code locations} argument. Only part of
{
* the file is extracted. Top level directories not present in {@code locations} will be omitted.
Unix
.
createSymbolicLink
(
entry
.
getLinkName
(),
*
entryFile
.
getPath
());
* @param locations maps top level directory names to directories where those should be extracted
}
*/
}
else
if
(
entry
.
isFile
())
public
void
extract
(
final
Map
<
String
,
File
>
locations
)
throws
IOException
{
final
List
<
TarArchiveEntry
>
dirEntries
=
new
ArrayList
<
TarArchiveEntry
>();
TarArchiveEntry
entry
;
while
((
entry
=
in
.
getNextTarEntry
())
!=
null
)
{
final
File
entryFile
=
getEntryInLocation
(
entry
,
locations
);
if
(
entryFile
==
null
)
{
{
// We need to work around that isFile() is giving all
continue
;
// kinds of false positives on what is a file.
if
(
entry
.
isBlockDevice
()
||
entry
.
isCharacterDevice
()
||
entry
.
isFIFO
()
||
entry
.
isGNULongLinkEntry
()
||
entry
.
isGNULongNameEntry
()
||
entry
.
isGNUSparse
()
||
entry
.
isPaxHeader
()
||
entry
.
isGlobalPaxHeader
())
{
continue
;
}
if
(
entryFile
.
getParentFile
().
exists
()
==
false
)
{
entryFile
.
getParentFile
().
mkdirs
();
}
extractFileContent
(
entryFile
);
setFileMetadata
(
entryFile
,
entry
);
}
}
extractEntry
(
entry
,
entryFile
,
dirEntries
);
}
}
// Set directory metadata in reverse order, i.e. descendants first
// Set directory metadata in reverse order, i.e. descendants first
final
ListIterator
<
TarArchiveEntry
>
it
=
dirEntries
final
ListIterator
<
TarArchiveEntry
>
it
=
dirEntries
...
@@ -116,7 +112,58 @@ public class Untar implements Closeable
...
@@ -116,7 +112,58 @@ public class Untar implements Closeable
while
(
it
.
hasPrevious
())
while
(
it
.
hasPrevious
())
{
{
entry
=
it
.
previous
();
entry
=
it
.
previous
();
setFileMetadata
(
new
File
(
rootDirectory
,
entry
.
getName
()),
entry
);
File
entryFile
=
getEntryInLocation
(
entry
,
locations
);
setFileMetadata
(
entryFile
,
entry
);
}
}
public
File
getEntryInLocation
(
TarArchiveEntry
entry
,
final
Map
<
String
,
File
>
locations
)
{
String
[]
parts
=
entry
.
getName
().
split
(
"/"
,
2
);
String
head
=
parts
[
0
];
String
tail
=
parts
[
1
];
return
new
File
(
locations
.
get
(
head
),
tail
);
}
private
void
extractEntry
(
TarArchiveEntry
entry
,
final
File
entryFile
,
final
List
<
TarArchiveEntry
>
dirEntries
)
throws
FileNotFoundException
,
IOException
{
if
(
entry
.
isDirectory
())
{
dirEntries
.
add
(
entry
);
entryFile
.
mkdirs
();
}
else
if
(
entry
.
isLink
())
{
if
(
Unix
.
isOperational
())
{
Unix
.
createHardLink
(
entry
.
getLinkName
(),
entryFile
.
getPath
());
}
}
else
if
(
entry
.
isSymbolicLink
())
{
if
(
Unix
.
isOperational
())
{
Unix
.
createSymbolicLink
(
entry
.
getLinkName
(),
entryFile
.
getPath
());
}
}
else
if
(
entry
.
isFile
())
{
// We need to work around that isFile() is giving all
// kinds of false positives on what is a file.
if
(
entry
.
isBlockDevice
()
||
entry
.
isCharacterDevice
()
||
entry
.
isFIFO
()
||
entry
.
isGNULongLinkEntry
()
||
entry
.
isGNULongNameEntry
()
||
entry
.
isGNUSparse
()
||
entry
.
isPaxHeader
()
||
entry
.
isGlobalPaxHeader
())
{
return
;
}
if
(
entryFile
.
getParentFile
().
exists
()
==
false
)
{
entryFile
.
getParentFile
().
mkdirs
();
}
extractFileContent
(
entryFile
);
setFileMetadata
(
entryFile
,
entry
);
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment