From 64306957bf76de287ef45f9f3cd13d36e4eab9a9 Mon Sep 17 00:00:00 2001 From: "xiaoguang.wang" Date: Fri, 10 Jun 2016 01:44:38 +0800 Subject: [PATCH] zip & share --- .../androidtcpdumpgui/ViewActivity.java | 60 ++++++++++++++++--- .../androidtcpdumpgui/util/Io.java | 44 +++++++++++++- app/src/main/res/menu/menu_viewcapture.xml | 6 ++ 3 files changed, 101 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/voxlearning/androidtcpdumpgui/ViewActivity.java b/app/src/main/java/com/voxlearning/androidtcpdumpgui/ViewActivity.java index c63c442..a3b6efe 100644 --- a/app/src/main/java/com/voxlearning/androidtcpdumpgui/ViewActivity.java +++ b/app/src/main/java/com/voxlearning/androidtcpdumpgui/ViewActivity.java @@ -4,6 +4,7 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.net.Uri; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; @@ -14,6 +15,7 @@ import android.view.MenuItem; import android.view.View; import android.widget.EditText; +import android.widget.Toast; import com.voxlearning.androidtcpdumpgui.util.Io; import com.voxlearning.androidtcpdumpgui.util.Str; @@ -77,6 +79,10 @@ public boolean onOptionsItemSelected(MenuItem item) { doDelete(); return true; + case R.id.menu_share: + doSaveCommentZipShare(); + return true; + case android.R.id.home: doGoBack(); return true; @@ -148,18 +154,24 @@ public void onClick(DialogInterface dialog, int which) { b.show(); } + private void saveComment() { + String comment = mEditTextComment.getText().toString(); + saveComment(comment); + } + private void saveComment(String comment) { - String dir = CaptureFilePath.OutputDir(mCaptureName); - Io.writeFile(dir + "/" + CaptureFilePath.FileName_Comment, comment); - Io.writeFileAppend(dir + "/" + CaptureFilePath.FileName_Logs, CaptureManager.nowLogString() + " comment:" + comment + "\n"); + if(!Str.eq(comment, mLastComment)) { + String dir = CaptureFilePath.OutputDir(mCaptureName); + Io.writeFile(dir + "/" + CaptureFilePath.FileName_Comment, comment); + Io.writeFileAppend(dir + "/" + CaptureFilePath.FileName_Logs, CaptureManager.nowLogString() + " comment:" + comment + "\n"); + mLastComment = comment; + } } private void doGoBack() { final String comment = mEditTextComment.getText().toString(); if(Str.isEmpty(mLastComment)) { - if(!Str.isEmpty(comment)) { - saveComment(comment); - } + saveComment(comment); finish(); return; } @@ -190,5 +202,39 @@ public void onClick(DialogInterface dialog, int which) { b.show(); } -} + private void doSaveCommentZipShare() { + + saveComment(); + + String dirCapture = CaptureFilePath.OutputDir(mCaptureName); + String []files = new String[] { + dirCapture + "/" + CaptureFilePath.FileName_Start, + dirCapture + "/" + CaptureFilePath.FileName_Stop, + dirCapture + "/" + CaptureFilePath.FileName_Apps, + dirCapture + "/" + CaptureFilePath.FileName_Comment, + dirCapture + "/" + CaptureFilePath.FileName_Logs, + dirCapture + "/" + CaptureFilePath.FileName_Packets, + dirCapture + "/" + CaptureFilePath.FileName_TcpdumpOut, + }; + + + String dirTmpShare = CaptureFilePath.BaseDir() + "/tmp/share"; + Io.deleteFollowSymlink(dirTmpShare); + new File(dirTmpShare).mkdirs(); + + String tmpZipFilePath = dirTmpShare + "/" + mCaptureName + ".zip"; + if(!Io.zip(files, tmpZipFilePath)) { + Toast.makeText(this, "Failed to zip files", Toast.LENGTH_LONG).show(); + return; + } + + + Intent intentShareFile = new Intent(Intent.ACTION_SEND); + intentShareFile.setType("application/zip"); + intentShareFile.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + tmpZipFilePath)); + intentShareFile.putExtra(Intent.EXTRA_SUBJECT, "Tcpdump:" + mCaptureName); + intentShareFile.putExtra(Intent.EXTRA_TEXT, mLastComment); + startActivity(Intent.createChooser(intentShareFile, "Share Captured Data:" + mCaptureName)); + } +} diff --git a/app/src/main/java/com/voxlearning/androidtcpdumpgui/util/Io.java b/app/src/main/java/com/voxlearning/androidtcpdumpgui/util/Io.java index eda96a3..61f9726 100644 --- a/app/src/main/java/com/voxlearning/androidtcpdumpgui/util/Io.java +++ b/app/src/main/java/com/voxlearning/androidtcpdumpgui/util/Io.java @@ -6,6 +6,8 @@ import com.voxlearning.androidtcpdumpgui.CaptureFilePath; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.Closeable; import java.io.File; @@ -14,9 +16,13 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; public class Io { + static private final String TAG = "Io"; + static public void close(Closeable c) { if (c == null) return; @@ -111,7 +117,7 @@ static public boolean extractAssetFile(Context context, String assetFilePath, St InputStream is = null; FileOutputStream fos = null; - Log.i("Io", "extracting " + assetFilePath + " to " + outFilePath + " ..."); + Log.i(TAG, "extracting " + assetFilePath + " to " + outFilePath + " ..."); try { is = context.getAssets().open(assetFilePath); if (outFilePath.startsWith("/")) { @@ -126,11 +132,45 @@ static public boolean extractAssetFile(Context context, String assetFilePath, St } return true; } catch (IOException e) { - Log.e("Io", "can not extract " + assetFilePath + " to " + outFilePath, e); + Log.e(TAG, "can not extract " + assetFilePath + " to " + outFilePath, e); return false; } finally { Io.close(is); Io.close(fos); } } + + static public boolean zip(String[] files, String zipFile) { + ZipOutputStream out = null; + int BUFFER_SIZE = 4096; + byte data[] = new byte[BUFFER_SIZE]; + + try { + out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile))); + for (int i = 0; i < files.length; i++) { + FileInputStream fi = null; + try { + fi = new FileInputStream(files[i]); + BufferedInputStream origin = new BufferedInputStream(fi, BUFFER_SIZE); + ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1)); + out.putNextEntry(entry); + + int count; + while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) { + out.write(data, 0, count); + } + } finally { + close(fi); + } + } + return true; + } + catch (IOException e) { + Log.e(TAG, "can not zip files: " + e.getMessage(), e); + return false; + } + finally { + close(out); + } + } } diff --git a/app/src/main/res/menu/menu_viewcapture.xml b/app/src/main/res/menu/menu_viewcapture.xml index de1e61d..322a3f5 100644 --- a/app/src/main/res/menu/menu_viewcapture.xml +++ b/app/src/main/res/menu/menu_viewcapture.xml @@ -7,4 +7,10 @@ android:icon="@android:drawable/ic_menu_delete" android:title="Delete"> + + +