Guest User

Untitled

a guest
Jul 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package pl.sweternet.commander.filesystem;
  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.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.util.ArrayList;
  11. import java.util.HashSet;
  12. import java.util.List;
  13. import java.util.Set;
  14.  
  15. import pl.sweternet.commander.FileTableElement;
  16.  
  17. public class ResourceManipulator {
  18.  
  19.     private long size;
  20.     private long done;
  21.  
  22.     private List<File> sources;
  23.     private File target;
  24.  
  25.     Set<ProgressListener> listeners;
  26.  
  27.     public ResourceManipulator() {
  28.         listeners = new HashSet<ProgressListener>();
  29.         size = 0l;
  30.         done = 0l;
  31.     }
  32.  
  33.     public void addListener(ProgressListener listener) {
  34.         listeners.add(listener);
  35.     }
  36.  
  37.     private void broadcast() {
  38.         for (ProgressListener listener : listeners) {
  39.             listener.progress(done, size);
  40.         }
  41.     }
  42.  
  43.     public void copyTo(List<File> sources, File target) throws IOException {
  44.         System.out.println("Coping...");
  45.         List<File> files = new ArrayList<File>();
  46.  
  47.         String parentString="";
  48.        
  49.         for (File file : sources) {
  50.             parentString = file.getParent();
  51.             size += FileTableElement.getFileElement(file).getRealSize();
  52.             files.addAll(FileTableElement.getFileElement(file).getFiles());
  53.         }
  54.        
  55.         for (File file : files) {
  56.             String toCopy = file.getAbsolutePath();
  57.             toCopy = toCopy.replaceFirst(parentString, "");
  58.            
  59.             copyFile(file, new File(target, toCopy));
  60.         }
  61.     }
  62.  
  63.     private void copy(File source, File target) throws IOException {
  64.         if (source.isDirectory()) {
  65.             if (!target.exists()) {
  66.                 target.mkdir();
  67.             }
  68.  
  69.             String[] children = source.list();
  70.             for (String file : children) {
  71.                 File smth = new File(source, file);
  72.                 if (!smth.equals(target))
  73.                     copy(smth, new File(target, file));
  74.             }
  75.         } else {
  76.             copyFile(source, target);
  77.         }
  78.     }
  79.  
  80.     private void copyFile(File source, File target) throws IOException {
  81.         if (source.isDirectory())
  82.             throw new IllegalArgumentException();
  83.  
  84.         target.getParentFile().mkdirs();
  85.        
  86.         InputStream in = new FileInputStream(source);
  87.         OutputStream out = new FileOutputStream(target);
  88.  
  89.         // Copy the bits from instream to outstream
  90.         byte[] buf = new byte[1024];
  91.         int len;
  92.         while ((len = in.read(buf)) > 0) {
  93.             out.write(buf, 0, len);
  94.             done += len;
  95.             broadcast();
  96.         }
  97.         in.close();
  98.         out.close();
  99.     }
  100.  
  101. }
Add Comment
Please, Sign In to add comment