Skip to content

Commit

Permalink
- Fixed bug in decompression
Browse files Browse the repository at this point in the history
  • Loading branch information
kterry committed Dec 16, 2014
1 parent 84700b5 commit 391488f
Showing 1 changed file with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.URLConnection;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -855,12 +856,21 @@ private void getCASCertificates() throws IOException, ArchiveException {
}
}

private void writeCAsCertificates(InputStream in) throws IOException,
private void writeCAsCertificates(InputStream is) throws IOException,
ArchiveException {
// read tar from ESGF URL

// read tar
String tempPath = System.getProperty("java.io.tmpdir") + File.separator
+ "esg-certificates.tar";
File tarFile = new File(tempPath);
OutputStream ous = new FileOutputStream(tarFile);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
ous.write(buf, 0, len);
}
ous.close();
is.close();

// untar certificates
String dir = System.getProperty("java.io.tmpdir") + File.separator;
Expand Down Expand Up @@ -948,13 +958,19 @@ private static List<File> unTar(final File inputFile, final File outputDir)
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
.createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;

// map to save the links that their orig files already hasn't been read
Map<String, String> linkFileMap = new HashMap<String, String>();
String folderPath = "";

while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
final File outputFile = new File(outputDir, entry.getName());

if (entry.isDirectory()) {
LOG.debug(String.format(
"Attempting to write output directory %s.",
outputFile.getAbsolutePath()));
folderPath = outputFile.getAbsolutePath();
if (!outputFile.exists()) {
LOG.info(String.format(
"Attempting to create output directory %s.",
Expand All @@ -972,13 +988,23 @@ private static List<File> unTar(final File inputFile, final File outputDir)
outputFile);

if (entry.isSymbolicLink()) {
String symLinkFileName = entry.getLinkName();

IOUtils.copy(
new FileInputStream(new File(outputFile.getParent()
+ File.separator + symLinkFileName)),
outputFileStream);
outputFileStream.close();
// write all symlinks as files copying the content of their
// orig files
String origFile = entry.getLinkName();

try {
IOUtils.copy(
new FileInputStream(new File(outputFile
.getParent()
+ File.separator
+ origFile)), outputFileStream);
} catch (FileNotFoundException e) {
// if dest file of symlink already hasn't been write
// add in link-file map
linkFileMap.put(entry.getName(), origFile);
} finally {
outputFileStream.close();
}

} else {
IOUtils.copy(debInputStream, outputFileStream);
Expand All @@ -987,6 +1013,19 @@ private static List<File> unTar(final File inputFile, final File outputDir)
}
untaredFiles.add(outputFile);
}

// write all symlinks as files copying the content of their orig files
for (Map.Entry<String, String> entryMap : linkFileMap.entrySet()) {

String linkName = entryMap.getKey();
String origName = entryMap.getValue();

// copying origName file in a new file with linkName
IOUtils.copy(new FileInputStream(new File(folderPath
+ File.separator + origName)), new FileOutputStream(
new File(outputDir + File.separator + linkName)));
}

debInputStream.close();

return untaredFiles;
Expand Down

0 comments on commit 391488f

Please sign in to comment.