在 react-native 上解压缩文件的任何方法
Any way to unzip file on react-native
设法将 .zip 文件下载到我移动设备上的文件系统 phone。但过了一会儿意识到我找不到解压缩该文件的方法。正如我尝试的那样:
第一个请求后立即死亡,出现错误"Cannot read property 'unzip' of undefined"(仔细遵循说明)
第二个死掉了,因为它依赖于 codrova 端口来响应本机,这也不起作用。
有什么解决这些问题的建议或方法吗?
使用 react-native 0.35,用 android 5.1.1 在 Note4 上测试。
我确实设法解决了我的问题:
使用react-native-zip-archive
解决办法是更改里面的代码:
RNZipArchiveModule.java 模块内的文件
需要应用的更改写在这条评论中:
https://github.com/plrthink/react-native-zip-archive/issues/14#issuecomment-261712319
所以解决问题归功于胡九德阳
往这个方向走:
node_modules\react-native-zip-archive\android\src\main\java\com\rnziparchive\RNZipArchiveModule.java
并替换此代码而不是解压缩方法
public static void customUnzip(File zipFile, File targetDirectory) throws IOException {
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(new FileInputStream(zipFile)));
try {
ZipEntry ze;
int count;
byte[] buffer = new byte[8192];
while ((ze = zis.getNextEntry()) != null) {
File file = new File(targetDirectory, ze.getName());
File dir = ze.isDirectory() ? file : file.getParentFile();
if (!dir.isDirectory() && !dir.mkdirs())
throw new FileNotFoundException("Failed to ensure directory: " +
dir.getAbsolutePath());
if (ze.isDirectory())
continue;
FileOutputStream fout = new FileOutputStream(file);
try {
while ((count = zis.read(buffer)) != -1)
fout.write(buffer, 0, count);
} finally {
fout.close();
}
/* if time should be restored as well
long time = ze.getTime();
if (time > 0)
file.setLastModified(time);
*/
}
} finally {
zis.close();
}
}
//**************************
@ReactMethod
public void unzip(final String zipFilePath, final String destDirectory, final String charset, final Promise promise) {
new Thread(new Runnable() {
@Override
public void run() {
try {
customUnzip(new File(zipFilePath ) , new File(destDirectory));
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
设法将 .zip 文件下载到我移动设备上的文件系统 phone。但过了一会儿意识到我找不到解压缩该文件的方法。正如我尝试的那样:
第一个请求后立即死亡,出现错误"Cannot read property 'unzip' of undefined"(仔细遵循说明)
第二个死掉了,因为它依赖于 codrova 端口来响应本机,这也不起作用。
有什么解决这些问题的建议或方法吗?
使用 react-native 0.35,用 android 5.1.1 在 Note4 上测试。
我确实设法解决了我的问题:
使用react-native-zip-archive
解决办法是更改里面的代码: RNZipArchiveModule.java 模块内的文件
需要应用的更改写在这条评论中: https://github.com/plrthink/react-native-zip-archive/issues/14#issuecomment-261712319
所以解决问题归功于胡九德阳
往这个方向走: node_modules\react-native-zip-archive\android\src\main\java\com\rnziparchive\RNZipArchiveModule.java
并替换此代码而不是解压缩方法
public static void customUnzip(File zipFile, File targetDirectory) throws IOException {
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream(new FileInputStream(zipFile)));
try {
ZipEntry ze;
int count;
byte[] buffer = new byte[8192];
while ((ze = zis.getNextEntry()) != null) {
File file = new File(targetDirectory, ze.getName());
File dir = ze.isDirectory() ? file : file.getParentFile();
if (!dir.isDirectory() && !dir.mkdirs())
throw new FileNotFoundException("Failed to ensure directory: " +
dir.getAbsolutePath());
if (ze.isDirectory())
continue;
FileOutputStream fout = new FileOutputStream(file);
try {
while ((count = zis.read(buffer)) != -1)
fout.write(buffer, 0, count);
} finally {
fout.close();
}
/* if time should be restored as well
long time = ze.getTime();
if (time > 0)
file.setLastModified(time);
*/
}
} finally {
zis.close();
}
}
//**************************
@ReactMethod
public void unzip(final String zipFilePath, final String destDirectory, final String charset, final Promise promise) {
new Thread(new Runnable() {
@Override
public void run() {
try {
customUnzip(new File(zipFilePath ) , new File(destDirectory));
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}