SHARE
TWEET

Untitled

a guest Jul 12th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main;
  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.FileWriter;
  8. import java.io.IOException;
  9.  
  10. import cse332.jazzlib.ZipEntry;
  11. import cse332.jazzlib.ZipOutputStream;
  12.  
  13. /*
  14.  * The Zip program requires a working SuffixTrie implementation. If you haven't
  15.  * completed SuffixTrie, you will need to delete/move/rename SuffixTrie.java in
  16.  * order for Eclipse to pick up and use the provided JAR file.
  17.  */
  18.  
  19. public class Zip {
  20.  
  21.     /** Larger -> better compression ratio, slower runtime **/
  22.     public static int BUFFER_LENGTH = 30;
  23.  
  24.     /** This constant will spit out compression debug output if turned on **/
  25.     public static final boolean DEBUG_OUTPUT = false;
  26.  
  27.    
  28.     /** Constants that define the input and output **/
  29.     public static final String ZIP_NAME = "test.zip";
  30.     public static final String[] FILES_TO_COMPRESS = {
  31.         "amazon.html",
  32.         "bing.html",
  33.         "facebook.html",
  34.         "google.html",
  35.         "twitter.html"
  36.     };
  37.  
  38.     public static void main(String[] args) throws IOException {
  39.        
  40.         FileWriter times = new FileWriter("./time_results.csv");
  41.         FileWriter ratios = new FileWriter("./ratios_results.csv");
  42.  
  43.         times.write("FileName");
  44.         ratios.write("FileName");
  45.  
  46.         for (int buffersize = 10; buffersize < 5000; buffersize += 10) {
  47.             times.write("," + Integer.toString(buffersize));
  48.             ratios.write("," + Integer.toString(buffersize));
  49.         }
  50.  
  51.         times.write("\n");
  52.         ratios.write("\n");
  53.  
  54.         for (int buffersize = 10; buffersize < 5000; buffersize += 10) {
  55.             BUFFER_LENGTH = buffersize;
  56.  
  57.             for (String file : FILES_TO_COMPRESS) {
  58.                 FileOutputStream fos = new FileOutputStream(file + ".zip");
  59.                 ZipOutputStream zos = new ZipOutputStream(fos);
  60.    
  61.                 long startTime = System.nanoTime();
  62.    
  63.                 addToZipFile(file, zos);
  64.    
  65.                 long endTime = System.nanoTime();
  66.                 long duration = (endTime - startTime);
  67.  
  68.                 File newZipFile = new File(file + ".zip");
  69.                
  70.                 ratios.write(Long.toString(newZipFile.length()) + ",");
  71.                 times.write(Long.toString(duration) + ",");
  72.    
  73.                 zos.close();
  74.                 fos.close();
  75.             }
  76.  
  77.             times.write("\n");
  78.             ratios.write("\n");
  79.  
  80.         }
  81.  
  82.         times.close();
  83.         ratios.close();
  84.  
  85.        
  86.        
  87.     }
  88.  
  89.     public static void addToZipFile(String fileName, ZipOutputStream zos) throws FileNotFoundException, IOException {
  90.         System.out.println("Writing '" + fileName + "' to zip file");
  91.  
  92.         File file = new File(fileName);
  93.         FileInputStream fis = new FileInputStream(file);
  94.         ZipEntry zipEntry = new ZipEntry(fileName);
  95.         zos.putNextEntry(zipEntry);
  96.  
  97.         byte[] bytes = new byte[1024];
  98.         int length;
  99.         int i = 0;
  100.         while ((length = fis.read(bytes)) >= 0) {
  101.             zos.write(bytes, 0, length);
  102.             // System.out.print("Wrote 1024 bytes...");
  103.             i++;
  104.             if (i % 100 == 0) {
  105.                 System.out.println();
  106.             }
  107.         }
  108.  
  109.         zos.closeEntry();
  110.         fis.close();
  111.  
  112.         System.out.println("Done!");
  113.     }
  114.  
  115. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top