zip ファイルを解凍しようとしました (このファイルには多くのサブフォルダーとファイルが含まれています)。
I am not able to create sub-folders while unzipping the file.
次のようなエラーが表示されるたびに:
No such file or directory.
私はこれについて多くのことを検索しました:
- Android - フォルダを解凍しますか?
- http://www.roseindia.net/tutorial/java/corejava/zip/ZipIsDirectory.html
- Android アプリケーションで sd カード上の zip ファイルを解凍します
- Javaで再帰的にファイルを解凍するには?
しかし、何も私を助けませんでした。
以下は私が試したことです:
public class UnZipper {
private static final String TAG = "UnZip";
private String mFileName, mDestinationPath;
public UnZipper(String fileName, String destinationPath) {
mFileName = fileName;
mDestinationPath = destinationPath;
}
public String getFileName() {
return mFileName;
}
public String getDestinationPath() {
return mDestinationPath;
}
// shrikant
public void unzip() {
String fullPath = mFileName;
Log.d(TAG, "unzipping " + mFileName + " to " + mDestinationPath);
doInBackground(fullPath, mDestinationPath);
}
// shrikant: I have changed return type from Boolean to boolean.
protected boolean doInBackground(String filePath, String destinationPath) {
File archive = new File(filePath);
boolean returnValue = false;
try {
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
ZipEntry entry = (ZipEntry) e.nextElement();
try {
unzipEntry(zipfile, entry, destinationPath);
Log.d("Unzipped", entry.getName());
returnValue = true;
} catch (Exception ex) {
Log.e(TAG,
"Error while extracting file: " + entry
+ ex.getMessage());
}
}
} catch (Exception e) {
Log.e(TAG, "Error while extracting file " + archive, e);
// return false;
}
return returnValue;
}
// shrikant: I have changed return type from void to boolean.
/**
* Unzips the zipped file into outputDir path.
*
* @param zipfile
* @param entry
* @param outputDir
* @throws IOException
*/
private void unzipEntry(ZipFile zipfile, ZipEntry entry, String outputDir)
throws IOException {
Log.d("CURRENT ZIP", entry.getName());
String _dir = null, fileName = null;
if (entry.getName().contains("\\")) {
_dir = entry.getName().substring(0, entry.getName().indexOf('\\'));
createDir(new File(outputDir, _dir));
fileName = entry.getName().substring(entry.getName().indexOf('\\'));
}
// Change by Prashant : To Remove "/" from file Name Date : 5/01/2011
if (fileName.toString().startsWith("\\")) {
fileName = fileName.substring(1); // End
}
if (_dir != "")
outputDir = outputDir + "/" + _dir;
File outputFile = new File(outputDir, fileName);
if (!outputFile.getParentFile().exists()) {
createDir(outputFile.getParentFile());
}
Log.d("OUTPUT FILE", outputDir + fileName);
Log.v(TAG, "Extracting: " + entry);
Log.d("FOUND inside unzipEntry()", entry.getName());
BufferedInputStream inputStream = new BufferedInputStream(
zipfile.getInputStream(entry));
// **here I am getting error.**
BufferedOutputStream outputStream = new BufferedOutputStream(
new FileOutputStream(outputFile));
// **above line.**
try {
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("Cannot create dir " + dir);
}
}
private void copy(BufferedInputStream input, BufferedOutputStream output)
throws IOException {
byte[] buffer = new byte[4096];
int size;
while ((size = input.read(buffer)) != -1)
output.write(buffer, 0, size);
}
}
私の質問は: (コードを見てください)
を呼び出しunzipEntry()
ているときに、サブフォルダーに遭遇すると、「/data/abc.ext
」のようなものを渡しますが、私のファイル システムには「data」という名前のフォルダーが含まれていません。作成しようとしても失敗しました。
では、圧縮されたファイルから宛先パスへのサブフォルダーを作成する方法は??
私も方法を試しました:
if(entry.isDirectory) {
// create directory
}
unzipEntry()
ただし、 (参照してくださいfor() loop
) は直接サブフォルダーの下のファイルを渡すため、これは呼び出されません。
これについて私を助けてください。
ありがとうございました。