using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Phone.Controls;
using Salient.SharpZipLib.Zip;

namespace WPSilverlightZip2
{
    public partial class MainPage : PhoneApplicationPage
    {
        List<String> stringlist = new List<string> { 
            "123456789012345678901234567890123456789012345678901234567890",
            "234567890234567890234567890234567890234567890234567890234567",
            "345678990345678990345678990345678990345678990345678990345678" };
        
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnCompress_Click(object sender, RoutedEventArgs e)
        {
            IsolatedStorageFileStream isoStream = IsolatedStorageFile.GetUserStoreForApplication().CreateFile("hello.zip");
            ZipOutputStream zout = new ZipOutputStream(isoStream);
                
            zout.SetLevel(9);
            foreach (string entry in stringlist)
            {
                ZipEntry zipEntry = new ZipEntry(entry);
                zipEntry.DateTime = DateTime.Now;
                byte[] content = System.Text.Encoding.UTF8.GetBytes(entry);
                zipEntry.Size = content.Length;

                zout.PutNextEntry(zipEntry);
                zout.Write(content, 0, content.Length);
                zout.CloseEntry();
            }
            zout.Flush();
            zout.Close();
            isoStream.Close();
            btnDecompress.IsEnabled = true;
        }

        private void btnDecompress_Click(object sender, RoutedEventArgs e)
        {
            IsolatedZipFile zipFile = new IsolatedZipFile("hello.zip");
            foreach (ZipEntry zipEntry in zipFile)
            {
                if (zipEntry.IsDirectory)
                {
                    continue;
                }
                Stream zipStream = zipFile.GetInputStream(zipEntry);
                byte[] content = new  byte[zipEntry.Size];
                zipStream.Read(content, 0, content.Length);
                zipStream.Close();

                MessageBox.Show(System.Text.Encoding.UTF8.GetString(content, 0, content.Length));
            }
            zipFile.Close();
        }
    }
}
