मेरे पास मेरे एसडी कार्ड पर एक ज़िप फ़ोल्डर है, मैं फ़ोल्डर को कैसे अनजिप कर सकता हूं (मेरे आवेदन कोड के भीतर)?एंड्रॉइड - एक फ़ोल्डर को अनजिप करें?
उत्तर
static Handler myHandler;
ProgressDialog myProgress;
public void unzipFile(File zipfile) {
myProgress = ProgressDialog.show(getContext(), "Extract Zip",
"Extracting Files...", true, false);
File zipFile = zipfile;
String directory = null;
directory = zipFile.getParent();
directory = directory + "/";
myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// process incoming messages here
switch (msg.what) {
case 0:
// update progress bar
myProgress.setMessage("" + (String) msg.obj);
break;
case 1:
myProgress.cancel();
Toast toast = Toast.makeText(getContext(),
"Zip extracted successfully",
Toast.LENGTH_SHORT);
toast.show();
provider.refresh();
break;
case 2:
myProgress.cancel();
break;
}
super.handleMessage(msg);
}
};
Thread workthread = new Thread(new UnZip(zipFile, directory));
workthread.start();
}
public class UnZip implements Runnable {
File archive;
String outputDir;
public UnZip(File ziparchive, String directory) {
archive = ziparchive;
outputDir = directory;
}
public void log(String log) {
Log.v("unzip", log);
}
@SuppressWarnings("unchecked")
public void run() {
Message msg;
try {
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries();
e.hasMoreElements();) {
ZipEntry entry = (ZipEntry) e.nextElement();
msg = new Message();
msg.what = 0;
msg.obj = "Extracting " + entry.getName();
myHandler.sendMessage(msg);
unzipEntry(zipfile, entry, outputDir);
}
} catch (Exception e) {
log("Error while extracting file " + archive);
}
msg = new Message();
msg.what = 1;
myHandler.sendMessage(msg);
}
@SuppressWarnings("unchecked")
public void unzipArchive(File archive, String outputDir) {
try {
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries();
e.hasMoreElements();) {
ZipEntry entry = (ZipEntry) e.nextElement();
unzipEntry(zipfile, entry, outputDir);
}
} catch (Exception e) {
log("Error while extracting file " + archive);
}
}
private void unzipEntry(ZipFile zipfile, ZipEntry entry,
String outputDir) throws IOException {
if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
return;
}
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
createDir(outputFile.getParentFile());
}
log("Extracting: " + entry);
BufferedInputStream inputStream = new
BufferedInputStream(zipfile
.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(
new FileOutputStream(outputFile));
try {
IOUtils.copy(inputStream, outputStream);
} finally {
outputStream.close();
inputStream.close();
}
}
private void createDir(File dir) {
log("Creating dir " + dir.getName());
if (!dir.mkdirs())
throw new RuntimeException("Can not create dir " + dir);
}
}
यह वही मेरे लिए काम किया धन्यवाद लोग
हैंडल मैसेज() में केस 2 क्या है? मुझे कोई भी संदेश नहीं दिख रहा है। क्या = 2. – Turnsole
यह मेरे लिए काम करता है, लेकिन अगर मैं सर्वर से ज़िप फ़ाइल डाउनलोड करता हूं तो उसे अनजिप करता हूं, यह मुझे त्रुटि दिखाता है java.util.zip.ZipException: EOCD नहीं मिला है, है इसके बारे में कोई विचार –
मैं प्रारंभिक विधि है कि AsyncTask प्रदान करता है और मुख्य थ्रेड पर पर्यवेक्षकों को अपडेट कर सकते का एक संशोधित संस्करण का उपयोग कर रहा है। बाइट संपीड़न द्वारा बाइट बेहद धीमा है और इससे बचा जाना चाहिए। इसके बजाय आउटपुट स्ट्रीम में डेटा के बड़े हिस्से की प्रतिलिपि बनाना एक और अधिक कुशल तरीका है।
private void unzipWebFile(String filename) {
String unzipLocation = getExternalFilesDir(null) + "/unzipped";
String filePath = Environment.getExternalStorageDirectory().toString();
UnZipper unzipper = new UnZipper(filename, filePath, unzipLocation);
unzipper.addObserver(this);
unzipper.unzip();
}
आपका पर्यवेक्षक एक update(Observable observable, Object data)
कॉलबैक जब unzip खत्म हो जाएगा:
package com.blarg.webviewscroller;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Observable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.IOUtils;
import android.os.AsyncTask;
import android.util.Log;
public class UnZipper extends Observable {
private static final String TAG = "UnZip";
private String mFileName, mFilePath, mDestinationPath;
public UnZipper (String fileName, String filePath, String destinationPath) {
mFileName = fileName;
mFilePath = filePath;
mDestinationPath = destinationPath;
}
public String getFileName() {
return mFileName;
}
public String getFilePath() {
return mFilePath;
}
public String getDestinationPath() {
return mDestinationPath;
}
public void unzip() {
String fullPath = mFilePath + "/" + mFileName + ".zip";
Log.d(TAG, "unzipping " + mFileName + " to " + mDestinationPath);
new UnZipTask().execute(fullPath, mDestinationPath);
}
private class UnZipTask extends AsyncTask<String, Void, Boolean> {
@SuppressWarnings("rawtypes")
@Override
protected Boolean doInBackground(String... params) {
String filePath = params[0];
String destinationPath = params[1];
File archive = new File(filePath);
try {
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
ZipEntry entry = (ZipEntry) e.nextElement();
unzipEntry(zipfile, entry, destinationPath);
}
} catch (Exception e) {
Log.e(TAG, "Error while extracting file " + archive, e);
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
setChanged();
notifyObservers();
}
private void unzipEntry(ZipFile zipfile, ZipEntry entry,
String outputDir) throws IOException {
if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
return;
}
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
createDir(outputFile.getParentFile());
}
Log.v(TAG, "Extracting: " + entry);
BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
try {
IOUtils.copy(inputStream, outputStream);
} finally {
outputStream.close();
inputStream.close();
}
}
private void createDir(File dir) {
if (dir.exists()) {
return;
}
Log.v(TAG, "Creating dir " + dir.getName());
if (!dir.mkdirs()) {
throw new RuntimeException("Can not create dir " + dir);
}
}
}
}
यह एक वर्ग है, जैसे कि Observer
लागू करता है के द्वारा किया जाता है।
यह स्वीकार्य उत्तर होना चाहिए। – Prateek
IOUtils कहां प्राप्त करें और इसे कैसे इंस्टॉल करें? –
'IOUtils' अपाचे कॉमन्स IO का हिस्सा है https://commons.apache.org/proper/commons-io/ –
बस के लिए @ "ऐड-ऑन" जवाब rich.e:
doInBackground()
में ZipEtries के माध्यम से दोहराने के बाद आप फ़ाइल को बंद कर देना चाहिए, क्योंकि कभी-कभी आप इसे अनज़िप करने के बाद फ़ाइल को नष्ट करना चाहते हैं और यह एक अपवाद फेंकता है तो फ़ाइल था बंद नहीं:
try {
ZipFile zipfile = new ZipFile(archive);
int entries = zipfile.size();
int total = 0;
if(onZipListener != null)
onZipListener.onUncompressStart(archive);
for (Enumeration<?> e = zipfile.entries(); e.hasMoreElements();) {
ZipEntry entry = (ZipEntry) e.nextElement();
if(onZipListener != null)
onZipListener.onUncompressProgress(archive, (int) (total++ * 100/entries));
unzipEntry(zipfile, entry, path);
}
zipfile.close();
} catch (Exception e) {
e.printStackTrace();
}
- 1. जावा ज़िप - फ़ोल्डर को अनजिप कैसे करें?
- 2. अनजिप किए गए फ़ोल्डर सामग्री
- 3. मैं एक विशिष्ट फ़ोल्डर को कैसे अनजिप कर सकता हूं?
- 4. विशिष्ट एक्सटेंशन को अनजिप करें केवल
- 5. आर में tar.gz फ़ाइल को अनजिप करें?
- 6. फ़ाइलों को ज़िप और अनजिप कैसे करें?
- 7. आईओएस आईफोन एप्लिकेशन में फ़ोल्डर को अनजिप करने के लिए कैसे करें
- 8. एंड्रॉइड - पासवर्ड एन्कोड किए गए ज़िप फ़ाइलों को अनजिप करना
- 9. एक संपूर्ण फ़ोल्डर को कैसे विचलित करें?
- 10. एंड्रॉइड में फ़ोल्डर का चयन कैसे करें?
- 11. पायथन 2.4 के साथ फ़ाइल को अनजिप कैसे करें?
- 12. ज़िप फ़ाइल को अनजिप करने के लिए कैसे करें C#
- 13. Android में 7zip संग्रह को अनजिप कैसे करें?
- 14. एंड्रॉइड में संपत्ति फ़ोल्डर से छवि को कैसे प्रदर्शित करें?
- 15. फोनगैप का उपयोग कर एंड्रॉइड में एक फ़ोल्डर से एक फ़ोल्डर में फ़ाइल को कैसे कॉपी/कॉपी करें?
- 16. कमांड लाइन का उपयोग कर फ़ाइल को अनजिप कैसे करें?
- 17. रिमोट मशीन पर फ़ाइलों को कॉपी और अनजिप करें - चींटी
- 18. .htaccess फ़ोल्डर को एक यूआरएल
- 19. एक फ़ोल्डर
- 20. एएस 3 का उपयोग कर फ़ाइलों को अनजिप और सेव करें?
- 21. सभी ज़िप खोजें, और जगह में अनजिप करें - यूनिक्स
- 22. एंड्रॉइड संपत्ति फ़ोल्डर की सुरक्षा
- 23. एंड्रॉइड में एसेट्स फ़ोल्डर में एक (.txt) फ़ाइल सहेजें?
- 24. एंड्रॉइड डिवाइस पर फ़ोल्डर चुनें
- 25. चलती एंड्रॉइड एसडीके स्थापना फ़ोल्डर
- 26. WordprocessingML फ़ोल्डर को पठनीय docx
- 27. एक मौजूदा फ़ोल्डर को एक रिपॉजिटरी
- 28. सबफ़ोल्डर से विंडोज फ़ोल्डर बैच प्रतिलिपि फ़ाइलों को एक फ़ोल्डर
- 29. एमएस तैनाती - किसी वेबसाइट पर एक फ़ोल्डर को तैनात करें
- 30. एक अतिरिक्त/अलग माइग्रेशन फ़ोल्डर को कॉन्फ़िगर कैसे करें
मुझे लगता है कि आपने बाहरी संग्रहण तक पहुंचने की अनुमति जोड़ दी है? – Kurru