由 Android Studio 创建的 Zip 文件损坏的应用程序

问题描述 投票:0回答:1

我已经为智能手表(wearOs)开发了一个应用程序,可以创建拉链。该应用程序将数据写入 Txt,当它完成必须执行的任务时,它会压缩 Txt,创建 Zip。

在我安装了该应用程序的大多数智能手表中,它都可以正常工作。但是,在其他情况下,我的 Zips 已损坏。

在这种情况下,有时它可以让我毫无问题地解压缩,而在其他情况下我会收到错误(0x80004005)。

我压缩Txt文件的方式是:

private File zip(File[] files){
        File zipFile = null;
        try {
            sendHandlerMessage(0, COMPRESS_STATE);

            // Compress data block and write compressed data in temporal compressed file
            zipFile = new File(files[0].getParentFile(),files[0].getName().replace("txt","zip"));
            ZipOutputStream zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));

            executorZipService = Executors.newFixedThreadPool(files.length);
            for(int i = 0; i< files.length; i++){
                int finalI = i;
                executorZipService.execute(new Runnable() {
                    @Override
                    public void run() {
                        byte[] buffer = new byte[BUFFER_SIZE];
                        FileInputStream fileInputStream = null;
                        try {
                            ZipEntry entry = new ZipEntry(files[finalI].getName());
                            zipOutputStream.putNextEntry(entry);
                            fileInputStream = new FileInputStream(files[finalI]);

                            int count;
                            long fileSize, actualSize=0;
                            fileSize = files[finalI].length();

                            while ((count = fileInputStream.read(buffer)) > 0) {
                                zipOutputStream.write(buffer, 0, count);
                                actualSize=actualSize+count;
                                sendHandlerMessage((int) Math.ceil((actualSize/fileSize)*100),COMPRESS_STATE);
                                //Log.d("CUSTOM_TAG",""+ i++);
                            }

                            fileInputStream.close();
                            files[finalI].delete();
                        }catch (IOException e){
                            e.printStackTrace();
                        }
                    }
                });

            }

            executorZipService.shutdown();
            executorZipService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
            zipOutputStream.closeEntry();
            zipOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //sendInfoToMainActivity(Constants.SENDING_STATE);
        SystemClock.sleep(10);
        return zipFile;
    }

我不知道我需要改变什么。在某些手表上它可以正常工作,而在其他手表上却不能,所以我不知道为什么。

java android android-studio zip wear-os
1个回答
0
投票

要使用 Java 在 Android Studio 中创建 ZIP 文件,

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.ZipOutputStream;

public class ZipCreator {

    public static void main(String[] args) {
        // Specify the source directory and the name of the output ZIP file
        String sourceDirectory = "/path/to/source_directory";
        String zipFileName = "/path/to/output.zip";

        // Call the createZipFile method to create the ZIP file
        createZipFile(sourceDirectory, zipFileName);
    }

    public static void createZipFile(String sourceDirectory, String zipFileName) {
        try {
            File source = new File(sourceDirectory);
            File zipFile = new File(zipFileName);
            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);

            zipDirectory(source, source, zos);

            zos.close();
            fos.close();
            System.out.println("ZIP file created successfully: " + zipFileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void zipDirectory(File source, File basePath, ZipOutputStream zos) throws IOException {
        File[] files = source.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    zipDirectory(file, basePath, zos);
                } else {
                    String entryName = file.getPath().substring(basePath.getPath().length() + 1);
                    ZipEntry zipEntry = new ZipEntry(entryName);

                    zos.putNextEntry(zipEntry);

                    FileInputStream fis = new FileInputStream(file);
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = fis.read(buffer)) > 0) {
                        zos.write(buffer, 0, length);
                    }

                    fis.close();
                    zos.closeEntry();
                }
            }
        }
    }
}

确保将 /path/to/source_directory 替换为要压缩的目录的路径,并将 /path/to/output.zip 替换为所需输出 ZIP 文件的路径和名称。

此代码定义了一个带有 createZipFile 方法的 ZipCreator 类,该方法从源目录及其子目录创建 ZIP 文件。 zipDirectory 方法用于递归遍历源目录并将文件添加到 ZIP 中。 ZIP 文件是使用 java.util.zip.ZipOutputStream 创建的。

希望有帮助

© www.soinside.com 2019 - 2024. All rights reserved.