我有这个代码应该解压缩文件。
public class dec extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "hello, starting to unZipp!", 15500).show();
String location = Environment.getExternalStorageDirectory() + "/unzipped/";
/////////////////////////////////////////////////////////////////////
try {
ZipInputStream zin = new ZipInputStream(getAssets().open("totalkeys.zip"));
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Decompress", "My UNZIPPING: " + ze.getName());
if(ze.isDirectory()) {
dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(location + ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch(Exception e) {
Log.v("Decompress", "My ERROR: "+e.toString());
/// throws My ERROR: java.io.FileNotFoundException: /mnt/sdcard/unzipped/Eng_blue/altlayout.txt (No such file or directory)
/// and dies.
}
}
////////////////////////////////////////////////////
private void dirChecker(String dir) {
String location = Environment.getExternalStorageDirectory() + "/unzipped/";
File f = new File(location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
////////////////////////////////////////////////////
finish();
}
}
然而它只会抛出“ava.io.FileNotFoundException:/mnt/sdcard/unzipped/Eng_blue/altlayout.txt”这是第一个文件的名称而死掉。
我认为dirChecker()方法会在流程上创建这个文件夹...任何想法如何解决?
谢谢!
答案 0 :(得分:1)
确保您已将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
权限添加到AndroidManifest.xml文件
答案 1 :(得分:1)
使用此:
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="test";
mFilePath = filePath="sdcard/folder";
mDestinationPath = destinationPath="/sdcard/folder1/";
}
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 {
} 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);
}
}
}