SharpZipLib来压缩文件

时间:2014-02-06 02:49:44

标签: c# android .net xamarin

我在Xamarin中为我的Android应用程序制作了一个sharpziplib,它从系统中的任何地方(用户输入)接收文件,并根据用户调用的方法(在应用程序中)压缩该文件。

类ZipOutputStream有一个压缩文件的Zip方法。

但是这个方法中的代码似乎不足以进行必要的操作。有人可以建议使用该方法成功压缩文件并将其放在系统中的特定位置吗?

申请代码:

string t;
protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    SetContentView (Resource.Layout.Main);

    EditText text = FindViewById<EditText>(Resource.Id.editText1);
    t = text.ToString ();

    text.KeyPress += (object sender, View.KeyEventArgs e) => {
        e.Handled = false;


    Button button1 = FindViewById<Button> (Resource.Id.button1);
    button1.Click += delegate {

        zipFile();

    };

    Button button2 = FindViewById<Button> (Resource.Id.button2);
    button2.Click += delegate {

        unzipFile();

    };

}

 string text1;

public void zipFile()
{

    ZipOutputStream.Zip (t, text1, 128);

}

public void unzipFile()
{
    ZipInputStream.UnZip (t, text1, 128);

}

}

库:

 public class ZipOutputStream 
{
public static void Zip(string SrcFile, string DstFile, int BufferSize)
{

    FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);
    FileStream fileStreamOut = new FileStream(DstFile, FileMode.Create, FileAccess.Write);
    ZipOutputStream zipOutStream = new ZipOutputStream(fileStreamOut);
    string path = DstFile;

    //string filenameNoExtension = Path.GetFileNameWithoutExtension(path);

    byte[] buffer = new byte[BufferSize];
    ZipEntry entry = new ZipEntry(Path.GetFileName(SrcFile));
    zipOutStream.PutNextEntry(entry);
    int size;
    do
    {
        size = fileStreamIn.Read(buffer, 0, buffer.Length);
        zipOutStream.Write(buffer, 0, size);
    } while (size > 0);

    string extension = Path.GetExtension(path);
    string filename = Path.GetFileName(path);
    Console.WriteLine("{0}\n{1}\n{2}\n{3}", extension,filename);

    zipOutStream.Close();
    fileStreamOut.Close();
    fileStreamIn.Close();
}}

0 个答案:

没有答案