Advertisement
RaWRCoder

kek

Apr 1st, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package com.gdev;
  2.  
  3. import java.io.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8.  
  9. /**
  10.  * Created by Андрей on 27.03.2015.
  11.  */
  12. public class ZipWorker implements IFileWorker {
  13.     TaskQueue queue;
  14.     public ZipWorker(TaskQueue tq)
  15.     {
  16.         queue = tq;
  17.     }
  18.  
  19.     private long doneData = 0;
  20.     @Override
  21.     public long getProgress() {
  22.         return doneData;
  23.     }
  24.  
  25.     private void iterateAllFiles(File path) {
  26.         try {
  27.             FileOutputStream fos = new FileOutputStream(path.getAbsolutePath() + "/" + path.getName() + ".zip");
  28.             ZipOutputStream zos = new ZipOutputStream(fos);
  29.  
  30.             for (File f : path.listFiles()) {
  31.                 if (f.isDirectory()) {
  32.                     System.out.println("Pushing directory '" + f.getName() + "' as a task...");
  33.                     queue.pushTask(f);
  34.                 }
  35.                 else if (f.getName() != path.getName() + ".zip")
  36.                     addToZip(path, f, zos);
  37.             }
  38.  
  39.             zos.close();
  40.             fos.close();
  41.         } catch (FileNotFoundException e) {
  42.             e.printStackTrace();
  43.         } catch (IOException e) {
  44.             e.printStackTrace();
  45.         }
  46.     }
  47.  
  48.     @Override
  49.     public void work(File f) {
  50.         if (f.isDirectory()) {
  51.             iterateAllFiles(f);
  52.         }
  53.     }
  54.  
  55.     private void writeZipFile(File directoryToZip, List<File> fileList) {
  56.         try {
  57.             FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip");
  58.             ZipOutputStream zos = new ZipOutputStream(fos);
  59.  
  60.             for (File file : fileList) {
  61.                 if (!file.isDirectory()) { // we only zip files, not directories
  62.                     addToZip(directoryToZip, file, zos);
  63.                     doneData += file.length();
  64.                 }
  65.             }
  66.  
  67.             zos.close();
  68.             fos.close();
  69.         } catch (FileNotFoundException e) {
  70.             e.printStackTrace();
  71.         } catch (IOException e) {
  72.             e.printStackTrace();
  73.         }
  74.     }
  75.  
  76.     private void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException,
  77.             IOException {
  78.         FileInputStream fis = new FileInputStream(file);
  79.  
  80.         // we want the zipEntry's path to be a relative path that is relative
  81.         // to the directory being zipped, so chop off the rest of the path
  82.         String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
  83.                 file.getCanonicalPath().length());
  84.         //System.out.println("Writing '" + zipFilePath + "' to zip file");
  85.         ZipEntry zipEntry = new ZipEntry(zipFilePath);
  86.         zos.putNextEntry(zipEntry);
  87.  
  88.         byte[] bytes = new byte[1024];
  89.         int length;
  90.         while ((length = fis.read(bytes)) >= 0) {
  91.             zos.write(bytes, 0, length);
  92.             doneData += length;
  93.         }
  94.  
  95.         zos.closeEntry();
  96.         fis.close();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement