Guest User

Untitled

a guest
May 27th, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Copyright (c) 2010, Ilya Strukov (iley@iley.ru)
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are met:
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. # * The name of the author nor the may not be used to endorse or promote products
  14. # derived from this software without specific prior written permission.
  15. #
  16. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. # DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
  20. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  
  27. # This script automatically detects archive type by file's extension and extracts
  28. # archive content. If there are several files in the archive and they are not
  29. # placed in a directory, script automatically creates directory for this files
  30. # before extracting. In case of directory name collision the scripts gives new
  31. # directories names like dirname_1, dirname_2 etc.
  32.  
  33. use Cwd 'abs_path';
  34. use strict;
  35. use warnings;
  36.  
  37. my %magic = ( '\.tar' => 'tar -xvf',
  38. '\.tar\.gz' => 'tar -xzvf',
  39. '\.tgz' => 'tar -xzvf',
  40. '\.gz' => 'gunzip',
  41. '\.tar\.bz2' => 'tar -xjvf',
  42. '\.zip' => 'unzip',
  43. '\.rar' => 'rar x',
  44. '\.7z' => '7z x'
  45. );
  46.  
  47. # check file for existance; generate new name if such file's already exists
  48. sub new_name {
  49. my ($file) = @_;
  50. if (-e $file) {
  51. my $i;
  52. $i = 1;
  53. (++$i) while -e "$file\_$i";
  54. $file = "$file\_$i";
  55. }
  56. return $file;
  57. }
  58.  
  59. sub file_info {
  60. my ($file) = @_;
  61. my ($cmd,$basename,$shortname) = ('','','');
  62.  
  63. foreach my $ex (keys %magic) {
  64. if ($file =~ /(([^\/]*)$ex)$/i) {
  65. $cmd = "$magic{$ex} " . abs_path($file);
  66. $basename = $1;
  67. $shortname = $2;
  68. last;
  69. }
  70. }
  71.  
  72. return ($cmd,$basename,$shortname);
  73. }
  74.  
  75. sub extract_file {
  76. my ($file) = @_;
  77. my $level = 0;
  78. my ($cmd,$basename,$shortname);
  79.  
  80. while (1) {
  81. ($cmd,$basename,$shortname) = &file_info($file);
  82. unless ($cmd) {
  83. die "File '$file' has unknow archive format" if $level == 0;
  84. last;
  85. }
  86.  
  87. my $tmpdir = &new_name(".extract_$basename");
  88.  
  89. (mkdir $tmpdir) or die "Cannot create temporary directory '$tmpdir'";
  90. chdir $tmpdir;
  91.  
  92. print "Executing '$cmd'";
  93. system $cmd;
  94. die "Child process exited abnormally" if $?;
  95.  
  96. chdir '..';
  97.  
  98. my $result = "";
  99. my @files = split(/\n/, `ls $tmpdir`);
  100. if (scalar(@files) == 1) {
  101. #if there was only one file in the archive, just move it to the destination directory
  102. $result = &new_name($files[0]);
  103. (rename "$tmpdir/$files[0]", $result) or die "Cannot rename file '$tmpdir/$files[0]' to '$result'";
  104. (rmdir $tmpdir) or die "Cannot remove temporary directory '$tmpdir'";
  105. } else {
  106. #if we have several files, rename temporary directory according to archive's name
  107. $result = &new_name($shortname);
  108. (rename $tmpdir, $result) or die "Cannot rename temporary directory '$tmpdir' to '$result'";
  109. }
  110.  
  111. print "Archive '$file' was extracted to '$result'\n";
  112. unlink $file if $level++ > 0;
  113.  
  114. if (-f $result) {
  115. $file = $result;
  116. } else {
  117. last;
  118. }
  119. }
  120. }
  121.  
  122. die "Usage: extract file" unless scalar(@ARGV) > 0;
  123. foreach my $param (@ARGV) {
  124. #TODO: process arguments
  125. &extract_file($param);
  126. }
Add Comment
Please, Sign In to add comment