#!/usr/bin/env ruby
# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH.unshift("../../compat") unless $LOAD_PATH.include?("../../compat")
$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "test/unit"
require "zip/zip"
require "qwik/util-path"

class TC_ZipLibrary < Test::Unit::TestCase
  def test_zip
    file = "test.zip"
    Zip::ZipOutputStream.open(file) {|zos|
      zos.put_next_entry("test/test.txt")
      zos.print("test")

      e = Zip::ZipEntry.new(file, "test2.txt")
      zos.put_next_entry(e)
      zos.print("test2")
    }

    zip = file.path.open {|f| f.read }
    assert_match(/\APK/, zip)
    assert_match(/test.txt/, zip)

    Zip::ZipInputStream.open(file) {|zis|
      e = zis.get_next_entry
      assert_equal("test/test.txt", e.name)
      assert_equal("test", zis.read)

      e = zis.get_next_entry
      assert_equal("test2.txt", e.name)
      assert_equal("test2", zis.read)

      e = zis.get_next_entry
      assert_equal(nil, e)
    }

    file.path.unlink
  end
end
