2013-08-23 7 views
6

मैंने स्मृति में कई बुफर्ड इमेज उत्पन्न किए हैं और मैं इसे ईमेल संलग्नक के रूप में भेजने से पहले उन्हें एक ज़िप फ़ाइल में संपीड़ित करना चाहता हूं। डिस्क से फ़ाइलों को पढ़ने के बिना मैं फ़ाइलों को ज़िप में कैसे सहेज सकता हूं।जावा के साथ डिस्क पर सहेजे बिना मैं ज़िप फ़ाइल कैसे उत्पन्न कर सकता हूं?

क्या कोई तरीका है कि मैं एक temp फ़ाइल बनाये बिना उन फ़ाइलों को संपीड़ित कर सकता हूं?

डिस्क पर लेखन हजारों फाइलों के निर्माण के कारण समय लेने वाला है।

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package cccprog; 

import java.awt.Component; 
import java.awt.Panel; 
import java.awt.event.KeyEvent; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import javax.imageio.ImageIO; 
import javax.swing.JFrame; 
import javax.swing.JRadioButton; 

/** 
* 
* @author Z 
*/ 
public class N { 

    public static void main(String[] args) throws Exception { 
     for (int i = 0; i < 10; i++) { 
      JFrame jf = new JFrame(); 
      Panel a = new Panel(); 

      JRadioButton birdButton = new JRadioButton(); 
      birdButton.setSize(100, 100); 

      birdButton.setSelected(true); 
      jf.add(birdButton); 

      getSaveSnapShot(birdButton, i + ".bmp"); 

     } 
    } 

    public static BufferedImage getScreenShot(Component component) { 

     BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_BYTE_GRAY); 
     // paints into image's Graphics 
     component.paint(image.getGraphics()); 
     return image; 
    } 

    public static void getSaveSnapShot(Component component, String fileName) throws Exception { 
     BufferedImage img = getScreenShot(component); 
//  BufferedImage img = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_BYTE_BINARY); 

     // write the captured image as a bmp 
     ImageIO.write(img, "bmp", new File(fileName)); 
    } 
} 
+3

java.utli.zip पर देखें। –

+1

बस मेरी जिज्ञासा के लिए, आप एक temp फ़ाइल बनाने से बचना क्यों चाहते हैं? –

+0

यह संबंधित लगता है http://stackoverflow.com/questions/7108035/how-to-flush-the-zipoutputstream-periodically-in-java – Ted

उत्तर

9

मैं यूज-केस तुम यहाँ कर रहे हैं, आप स्मृति में फ़ाइलों के हजारों आप स्मृति से बाहर नहीं बल्कि जल्दी से चला सकता है अगर के बारे में यकीन नहीं है।

हालांकि, ज़िप फ़ाइलें आमतौर पर वैसे भी धाराओं के साथ उत्पन्न कर रहे हैं, इसलिए उन्हें अस्थायी रूप से फ़ाइल में स्टोर करने के लिए कोई आवश्यकता नहीं है - और साथ स्मृति में हो या (केवल एक छोटा सा स्मृति बफर एक से बचने के लिए के साथ एक दूरदराज के प्राप्तकर्ता को सीधे ही स्ट्रीम हो सकता है बड़ी स्मृति पदचिह्न)।

मुझे कई साल पहले एक पुरानी ज़िप उपयोगिता मिली और इसे आपके उपयोग-मामले के लिए थोड़ा संशोधित किया गया। यह फ़ाइलों की सूची से बाइट सरणी में संग्रहीत एक ज़िप फ़ाइल बनाता है, जो बाइट एरे में भी संग्रहीत होता है। चूंकि आपके पास स्मृति में बहुत सारी फाइलें हैं, इसलिए मैंने केवल एक फ़ाइल नाम और सामग्री वाले बाइट सरणी के साथ एक छोटी सहायक श्रेणी MemoryFile जोड़ा। ओह, और मैंने बॉयलरप्लेट गेटर/सेटर सामान से बचने के लिए खेतों को सार्वजनिक किया - बस यहां कुछ जगह बचाने के लिए।

public class MyZip { 

    public static class MemoryFile { 
     public String fileName; 
     public byte[] contents; 
    } 

    public byte[] createZipByteArray(List<MemoryFile> memoryFiles) throws IOException { 
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
     ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream); 
     try { 
      for (MemoryFile memoryFile : memoryFiles) { 
       ZipEntry zipEntry = new ZipEntry(memoryFile.fileName); 
       zipOutputStream.putNextEntry(zipEntry); 
       zipOutputStream.write(memoryFile.contents); 
       zipOutputStream.closeEntry(); 
      } 
     } finally { 
      zipOutputStream.close(); 
     } 
     return byteArrayOutputStream.toByteArray(); 
    } 

} 
+0

मैंने अपनी मूल पोस्ट अपडेट की। मैं उलझन में था कि ऐसा करने के लिए एक बफर इमेज का उपयोग कैसे किया जा सकता था। – user1198289

+0

आपने अपनी मूल पोस्ट को "अपडेट" नहीं किया है, आपने इसे पूरी तरह बदल दिया है। मैं उस पर अत्यधिक शौकीन नहीं हूँ; यानी कि मैंने जवाब देने में समय व्यतीत करने के बाद सवाल पूरी तरह बदल दिया है। आपको अवगत होना चाहिए कि स्मृति में "हजारों" पीडीएफ और हजारों बुफर्ड इमेज दो अलग-अलग चीजें हैं। प्रत्येक छवि के लिए, प्रारूप (gif, png, jpg, आदि) में आउटपुट स्ट्रीम प्राप्त करने की आवश्यकता होगी जिसे आप उन्हें सहेजना/स्थानांतरित करना चाहते हैं। यह भी ध्यान रखें कि ज़िप छवियों को संपीड़न के मामले में थोड़ा प्रभाव पड़ता है, क्योंकि वे आमतौर पर पहले ही संपीड़ित होते हैं। – Steinar

1
   FileInputStream[] ins = //I assume you have all file handles in the form of FileInputStream 
      String[] fileNames = //I assume you have all file names 
      FileOutputStream out = new FileOutputStream(""); //specify the zip file name here 
      ZipOutputStream zipOut = new ZipOutputStream(out); 
      for (int i=0; i<ins.length; i++) { 
       ZipEntry entry = new ZipEntry(fileNames[i]); 
       zipOut.putNextEntry(entry); 
       int number = 0; 
       byte[] buffer = new byte[512]; 
       while ((number = ins[i].read(buffer)) != -1) { 
        zipOut.write(buffer, 0, number); 
       }       
       ins[i].close(); 
      } 
      out.close(); 
      zipOut.close(); 

दी गई जानकारी से आधार पर, मैं ऊपर कोड के साथ आते हैं। उम्मीद है की यह मदद करेगा।

+0

मैंने अपनी मूल पोस्ट अपडेट की। मैं उलझन में था कि ऐसा करने के लिए एक बफर इमेज का उपयोग कैसे किया जा सकता था। – user1198289

संबंधित मुद्दे

 संबंधित मुद्दे