2012-08-25 40 views
5

मैं अपने जावा ऐप में कुछ फाइलें डाउनलोड करता हूं और एक डाउनलोड मॉनिटर संवाद लागू करता हूं। लेकिन हाल ही में मैंने gzip के साथ सभी फ़ाइलों को संपीड़ित किया है और अब डाउनलोड मॉनीटर टूटा हुआ है।मॉनिटर GZip जावा में प्रगति डाउनलोड करें

मैं एक GZIPInputStream के रूप में फ़ाइल को खोलने और डाउनलोड हर kB के बाद डाउनलोड स्थिति अद्यतन। अगर फ़ाइल का आकार 1 एमबी है तो प्रगति बढ़ जाती है उदा। 4 एमबी जो असम्पीडित आकार है। मैं संपीड़ित डाउनलोड प्रगति की निगरानी करना चाहता हूं। क्या यह संभव है?

संपादित करें: स्पष्ट करने के लिए: मैं GZipInputStream से बाइट्स जो असम्पीडित बाइट्स हैं पढ़ रहा हूँ। तो यह मुझे अंत में सही फाइलसाइज नहीं देता है।

URL url = new URL(urlString); 
HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
con.connect(); 
... 
File file = new File("bibles/" + name + ".xml"); 
if(!file.exists()) 
    file.createNewFile(); 
out = new FileOutputStream(file); 
in = new BufferedInputStream(new GZIPInputStream(con.getInputStream())); 

byte[] buffer = new byte[1024]; 
int count; 
while((count = in.read(buffer)) != -1) { 
    out.write(buffer, 0, count); 
    downloaded += count; 
    this.stateChanged(); 
} 

... 

private void stateChanged() { 
    this.setChanged(); 
    this.notifyObservers(); 
} 

किसी भी मदद के लिए धन्यवाद:

यहाँ मेरी कोड है!

+0

मैं 'GZipInputStream' द्वारा डाउनलोड किए गए बाइट्स को पढ़ रहा हूं जो असम्पीडित स्ट्रीम है। तो यह वास्तविक फाइलसाइज नहीं है जिसे डाउनलोड किया गया है। – dbrettschneider

+0

ठीक है, अब मैं इसे देखता हूं। – SJuan76

उत्तर

4

विनिर्देश के अनुसार, GZIPInputStreamInflaterInputStream का उप-वर्ग है। InflaterInputStream में protected Inflater inf फ़ील्ड है जो Inflater है जो डिकंप्रेशन काम के लिए उपयोग किया जाता है। Inflater.getBytesRead आपके उद्देश्यों के लिए विशेष रूप से उपयोगी होना चाहिए।

दुर्भाग्य से, GZIPInputStreaminf का पर्दाफाश नहीं है, तो शायद आप अपने खुद के उपवर्ग बनाना होगा और Inflater का पर्दाफाश, उदा

public final class ExposedGZIPInputStream extends GZIPInputStream { 

    public ExposedGZIPInputStream(final InputStream stream) { 
    super(stream); 
    } 

    public ExposedGZIPInputStream(final InputStream stream, final int n) { 
    super(stream, n); 
    } 

    public Inflater inflater() { 
    return super.inf; 
    } 
} 
... 
final ExposedGZIPInputStream gzip = new ExposedGZIPInputStream(...); 
... 
final Inflater inflater = gzip.inflater(); 
final long read = inflater.getBytesRead(); 
+0

धन्यवाद! मैं तो बस() 'कि बनाता है' getBytesRead एक कस्टम GZIPInputStream 'inflater उपलब्ध int' के लागू करने के लिए किया था। – dbrettschneider

+0

धन्यवाद, यह वही है जो मैं ढूंढ रहा था! –