gpt4 book ai didi

Android UNZIP 输入流问题

转载 作者:搜寻专家 更新时间:2023-11-01 09:13:59 26 4
gpt4 key购买 nike

我正在尝试解压缩位于 Android 设备的 SDCARD 上的文件。如果 ZIP 文件只包含文件而不包含文件夹,则一切正常。但是,我希望应用程序解压缩的生产文件包含多个目录和子目录。这是我遇到问题的地方。

代码没有创建所需的文件夹结构。如果我在运行类之前手动创建文件夹,它会按计划解压缩,但将来我不知道目录结构并且需要代码来创建正确的文件夹结构。这是我正在使用的代码

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

public class HomeActivity extends Activity
{
private TextView tv;

private static final String ROOT_FOLDER = Environment.getExternalStorageDirectory()
+ File.separator + "FacebookPhotos";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
tv.append("Reading zip file from assets folder...");

// Start the unzipping process
thread.start();
}

private Thread thread = new Thread()
{

@Override

public void run()
{
sendMessage("-----------------------------------------------");

// Create a directory in the SDCard to store the files
File file = new File(ROOT_FOLDER);
if (!file.exists())
{
file.mkdirs();
}
else
{
file.delete();
}
try
{
// Open the ZipInputStream


ZipInputStream inputStream = new ZipInputStream(new FileInputStream("/sdcard/Photos/photos.zip"));


// Loop through all the files and folders
for (ZipEntry entry = inputStream.getNextEntry(); entry != null; entry = inputStream
.getNextEntry())
{
sendMessage("Extracting: " + entry.getName() + "...");

String innerFileName = ROOT_FOLDER + File.separator + entry.getName();

File innerFile = new File(innerFileName);
if (innerFile.exists())
{
innerFile.delete();
}

// Check if it is a folder
if (entry.isDirectory())


{
// Its a folder, create that folder
innerFile.mkdirs();

}
else
{
// Create a file output stream
FileOutputStream outputStream = new FileOutputStream(innerFileName);
final int BUFFER = 2048;

// Buffer the output to the file
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream,
BUFFER);

// Write the contents
int count = 0;
byte[] data = new byte[BUFFER];
while ((count = inputStream.read(data, 0, BUFFER)) != -1)
{
bufferedOutputStream.write(data, 0, count);
}

// Flush and close the buffers
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
sendMessage("DONE");

// Close the current entry
inputStream.closeEntry();
}
inputStream.close();
sendMessage("-----------------------------------------------");
sendMessage("Unzipping complete");

}
catch (IOException e)
{
sendMessage("Exception occured: " + e.getMessage());
e.printStackTrace();
}
}

};

private Handler handler = new Handler()
{

@Override
public void handleMessage(Message msg)
{
tv.append("\n" + msg.getData().getString("data"));
super.handleMessage(msg);
}

};

private void sendMessage(String text)
{
Message message = new Message();
Bundle data = new Bundle();
data.putString("data", text);
message.setData(data);
handler.sendMessage(message);
}
}

最佳答案

看看这个答案

Android - Unzip a folder?

我认为有时在 ZIP 文件中,目录条目可能会添加在其文件之后。在这种情况下,您不会在文件之前获得目录。链接示例为常规文件调用 file.getParentFile().exists() 并在不存在时创建该目录。

关于Android UNZIP 输入流问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6032786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com