Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. package io.github.zlika.reproducible;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.nio.file.Files;
  9. import java.nio.file.StandardCopyOption;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. import java.util.stream.Collectors;
  13.  
  14. import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
  15. import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
  16. import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
  17.  
  18. public class TarStripper extends AbstractStripper {
  19.  
  20. protected TarArchiveInputStream createInputStream(File in) throws FileNotFoundException, IOException {
  21. return new TarArchiveInputStream(new FileInputStream(in));
  22. }
  23.  
  24. protected TarArchiveOutputStream createOutputStream(File out) throws FileNotFoundException, IOException {
  25. return new TarArchiveOutputStream(new FileOutputStream(out));
  26. }
  27.  
  28. @Override
  29. public void strip(File in, File out) throws IOException {
  30. final File tmp = File.createTempFile("tmp-" + in.getName(), null);
  31. tmp.mkdir();
  32. tmp.deleteOnExit();
  33.  
  34. List<TarArchiveEntry> sortedNames = new ArrayList<>();
  35. try (final TarArchiveInputStream tar = createInputStream(in)) {
  36.  
  37. TarArchiveEntry entry;
  38. while ((entry = tar.getNextTarEntry()) != null) {
  39. sortedNames.add(entry);
  40. File copyTo = new File(tmp, entry.getName());
  41. if (entry.isDirectory()) {
  42. copyTo.mkdirs();
  43. } else {
  44. File destParent = copyTo.getParentFile();
  45. destParent.mkdirs();
  46.  
  47. final Stripper stripper = getSubFilter(entry.getName());
  48. if (stripper != null) {
  49. File dumpTo = new File(tmp, entry.getName() + ".pre");
  50. Files.copy(tar, dumpTo.toPath(), StandardCopyOption.REPLACE_EXISTING);
  51. stripper.strip(dumpTo, copyTo);
  52. } else {
  53. Files.copy(tar, copyTo.toPath(), StandardCopyOption.REPLACE_EXISTING);
  54. }
  55. }
  56. }
  57. }
  58. sortedNames = sortTarEntries(sortedNames);
  59. try (final TarArchiveOutputStream tout = createOutputStream(out)) {
  60. for (TarArchiveEntry entry : sortedNames) {
  61. File copyFrom = new File(tmp, entry.getName());
  62. final byte[] fileContent = Files.readAllBytes(copyFrom.toPath());
  63. entry.setSize(fileContent.length);
  64. tout.putArchiveEntry(filterTarEntry(entry));
  65. tout.write(fileContent);
  66. tout.closeArchiveEntry();
  67. }
  68. }
  69. }
  70.  
  71. private List<TarArchiveEntry> sortTarEntries(List<TarArchiveEntry> sortedNames) {
  72. sortedNames = sortedNames.stream().sorted((a, b) -> a.getName().compareTo(b.getName()))
  73. .collect(Collectors.toList());
  74. return sortedNames;
  75. }
  76.  
  77. private TarArchiveEntry filterTarEntry(TarArchiveEntry entry) {
  78. // Set times
  79. entry.setModTime(0);
  80. // Remove GID and UID that are not deterministic, to evaluate though
  81. entry.setGroupId(1000);
  82. entry.setUserId(1000);
  83. return entry;
  84. }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement