如何以编程方式在Android中压缩文件?

时间:2019-01-03 07:31:36

标签: android fileoutputstream dataoutputstream zipoutputstream

我知道这个问题已经存在,但是我不确定如何在我的情况下实现它...

首先,我创建了一个DataOutuputStream,它将以.uedl扩展名写入文件:

private static DataOutputStream os;
public static final String BASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myApp/logs/"


public static void startReceiveLogs() {
    if (debugBaudRate > -1) {
        mReadFlag = true;
        String time = TimeUtils.getCurrentTime(TimeZone.getDefault());
        try {
            os = new DataOutputStream(new FileOutputStream(BASE_PATH + time + "_logs.uedl"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mReceiveLogsThread = new Thread(new ReceiveLogsThread());
        mReceiveLogsThread.start();
    }
}

以下是写作内容:

        public void run() {
        byte[] rbuf = new byte[64];
        while (mReadFlag) {
            try {
                int len = mSerial.readLog(rbuf, mSerialPortLog);
                if (len > 0) {
                    os.write(rbuf, 0, len);
                }
            } catch (NullPointerException e1) {
                e1.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

一旦该线程停止运行,我想在相同路径下压缩该文件。

1 个答案:

答案 0 :(得分:0)

您尝试此操作将对您有所帮助

 public boolean zipFileAtPath(String sourcePath, String toLocation) {
    final int BUFFER = 2048;

    File sourceFile = new File(sourcePath);
    try {
        BufferedInputStream origin = null;
        FileOutputStream dest = new FileOutputStream(toLocation);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
                dest));
        if (sourceFile.isDirectory()) {
            zipSubFolder(out, sourceFile, sourceFile.getParent().length());
        } else {
            byte data[] = new byte[BUFFER];
            FileInputStream fi = new FileInputStream(sourcePath);
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath));
            entry.setTime(sourceFile.lastModified()); // to keep modification time after unzipping
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
        }
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}


 private void zipSubFolder(ZipOutputStream out, File folder,
                          int basePathLength) throws IOException {

    final int BUFFER = 2048;

    File[] fileList = folder.listFiles();
    BufferedInputStream origin = null;
    for (File file : fileList) {
        if (file.isDirectory()) {
            zipSubFolder(out, file, basePathLength);
        } else {
            byte data[] = new byte[BUFFER];
            String unmodifiedFilePath = file.getPath();
            String relativePath = unmodifiedFilePath
                    .substring(basePathLength);
            FileInputStream fi = new FileInputStream(unmodifiedFilePath);
            origin = new BufferedInputStream(fi, BUFFER);
            ZipEntry entry = new ZipEntry(relativePath);
            entry.setTime(file.lastModified()); // to keep modification time after unzipping
            out.putNextEntry(entry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
        }
    }
}

  public String getLastPathComponent(String filePath) {
    String[] segments = filePath.split("/");
    if (segments.length == 0)
        return "";
    String lastPathComponent = segments[segments.length - 1];
    return lastPathComponent;
}