diff --git a/bin/Zipper.class b/bin/Zipper.class new file mode 100644 index 0000000..ac5a0fd Binary files /dev/null and b/bin/Zipper.class differ diff --git a/bin/com/tryouts/producer_consumer/Consumer.class b/bin/com/tryouts/producer_consumer/Consumer.class new file mode 100644 index 0000000..1b5f73b Binary files /dev/null and b/bin/com/tryouts/producer_consumer/Consumer.class differ diff --git a/bin/com/tryouts/producer_consumer/Message.class b/bin/com/tryouts/producer_consumer/Message.class new file mode 100644 index 0000000..7595d65 Binary files /dev/null and b/bin/com/tryouts/producer_consumer/Message.class differ diff --git a/bin/com/tryouts/producer_consumer/Producer.class b/bin/com/tryouts/producer_consumer/Producer.class new file mode 100644 index 0000000..16c064b Binary files /dev/null and b/bin/com/tryouts/producer_consumer/Producer.class differ diff --git a/bin/com/tryouts/producer_consumer/Producer_Consumer.class b/bin/com/tryouts/producer_consumer/Producer_Consumer.class new file mode 100644 index 0000000..9f26a70 Binary files /dev/null and b/bin/com/tryouts/producer_consumer/Producer_Consumer.class differ diff --git a/src/Zipper.java b/src/Zipper.java new file mode 100644 index 0000000..78e11ee --- /dev/null +++ b/src/Zipper.java @@ -0,0 +1,86 @@ +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +public class Zipper +{ + List fileList; + private static final String OUTPUT_ZIP_FILE = "/Users/soumyajit/Desktop/Screenshots.zip"; + private static final String SOURCE_FOLDER = "/Users/soumyajit/Desktop/CIP_App/TestReports/Screenshot"; + + Zipper() + { + fileList = new ArrayList(); + } + public static void main(String [] args) + { + Zipper zip = new Zipper(); + zip.generateFileList(new File(SOURCE_FOLDER)); + zip.zipit(OUTPUT_ZIP_FILE); + } + + public void zipit(String zipFile) + { + byte[] buffer = new byte[1024]; + + try + { + FileOutputStream fos = new FileOutputStream(zipFile); + ZipOutputStream zos = new ZipOutputStream(fos); + + System.out.println("Output to zip: " +zipFile); + for(String file: this.fileList) + { + System.out.println("File added: " +file); + ZipEntry ze = new ZipEntry(file); + zos.putNextEntry(ze); + + FileInputStream fis = new FileInputStream(SOURCE_FOLDER + File.separator + file); + + int len; + while((len = fis.read(buffer)) > 0) + { + zos.write(buffer,0,len); + } + + fis.close(); + } + + zos.closeEntry(); + zos.close(); + + System.out.println("Done"); + } + catch(IOException e) + { + e.printStackTrace(); + } + } + + public void generateFileList(File node) + { + if(node.isFile()) + { + fileList.add(generateZipEntry(node.getAbsoluteFile().toString())); + } + + if(node.isDirectory()) + { + String[] subnode = node.list(); + for(String filename: subnode) + { + generateFileList(new File(node,filename)); + } + } + } + + String generateZipEntry(String file) + { + return file.substring(SOURCE_FOLDER.length()+1,file.length()); + } +} diff --git a/src/com/tryouts/producer_consumer/Consumer.java b/src/com/tryouts/producer_consumer/Consumer.java new file mode 100644 index 0000000..e0b2586 --- /dev/null +++ b/src/com/tryouts/producer_consumer/Consumer.java @@ -0,0 +1,31 @@ +package com.tryouts.producer_consumer; + +import java.util.concurrent.BlockingQueue; + +public class Consumer implements Runnable +{ +public BlockingQueue queue; + + public Consumer(BlockingQueue q) + { + this.queue = q; + } + + @Override + public void run() + { + try + { + Message msg; + while((msg = queue.take()).getMessage() != "exit") + { + Thread.sleep(10); + System.out.println("Consumed message: " +msg.getMessage()); + } + } + catch(Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/src/com/tryouts/producer_consumer/Message.java b/src/com/tryouts/producer_consumer/Message.java new file mode 100644 index 0000000..f5b1730 --- /dev/null +++ b/src/com/tryouts/producer_consumer/Message.java @@ -0,0 +1,15 @@ +package com.tryouts.producer_consumer; + +public class Message +{ + String msg; + public Message(String str) + { + this.msg = str; + } + + public String getMessage() + { + return msg; + } +} diff --git a/src/com/tryouts/producer_consumer/Producer.java b/src/com/tryouts/producer_consumer/Producer.java new file mode 100644 index 0000000..c47b4f9 --- /dev/null +++ b/src/com/tryouts/producer_consumer/Producer.java @@ -0,0 +1,31 @@ +package com.tryouts.producer_consumer; + +import java.util.concurrent.BlockingQueue; + +public class Producer implements Runnable +{ + public BlockingQueue queue; + + public Producer(BlockingQueue q) + { + this.queue = q; + } + @Override + public void run() + { + for(int i=0;i<100;i++) + { + Message msg = new Message(""+i); + try + { + Thread.sleep(i); + queue.put(msg); + System.out.println("Produced message: " +msg.getMessage()); + } + catch(InterruptedException e) + { + e.printStackTrace(); + } + } + } +} diff --git a/src/com/tryouts/producer_consumer/Producer_Consumer.java b/src/com/tryouts/producer_consumer/Producer_Consumer.java new file mode 100644 index 0000000..64b0588 --- /dev/null +++ b/src/com/tryouts/producer_consumer/Producer_Consumer.java @@ -0,0 +1,17 @@ +package com.tryouts.producer_consumer; + +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; + +public class Producer_Consumer +{ + public static void main(String [] args) + { + BlockingQueue queue = new ArrayBlockingQueue(2); + Producer prod = new Producer(queue); + Consumer con = new Consumer(queue); + new Thread(prod).start(); + new Thread(con).start(); + System.out.println("Producer / Consumer processes have been started"); + } +}