Skip to content

Commit

Permalink
zip & share
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Jun 9, 2016
1 parent 9eede13 commit 6430695
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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));
}
}
44 changes: 42 additions & 2 deletions app/src/main/java/com/voxlearning/androidtcpdumpgui/util/Io.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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("/")) {
Expand All @@ -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);
}
}
}
6 changes: 6 additions & 0 deletions app/src/main/res/menu/menu_viewcapture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@
android:icon="@android:drawable/ic_menu_delete"
android:title="Delete">
</item>

<item
android:id="@+id/menu_share"
android:icon="@android:drawable/ic_menu_share"
android:title="Share">
</item>
</menu>

0 comments on commit 6430695

Please sign in to comment.