zipada_58.0.0_2a0903e1/demo/demo_csv_into_zip.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
------------------------------------------------------------------------------
--  File:            Demo_csv_into_zip.adb
--  Description:     Demo / test / prototype derived from ZipTest.
--  Purpose:         Stuff many files directly into a Zip archive file.
--                     Can be helpful when using network drives, for instance:
--                     the numerous files can stay local, a single Zip file
--                     is stored on the network drive.
--  Date/version:    9-Jan-2013; 4-Feb-2009
--  Author:          Gautier de Montmollin
------------------------------------------------------------------------------

with Zip.Create;                        use Zip.Create;

with Ada.Characters.Handling;           use Ada.Characters.Handling;
with Ada.Text_IO;

procedure Demo_csv_into_zip is

  type Continent is
   (Overseas,
    North_America,
    Latin_America,
    Europe,
    Other
   );

  type GroupCountries is
   (France_Benelux,
    Northern_Europe,
    Central_and_Eastern_Europe,
    Southern_Europe,
    US,
    Canada_and_Greenland,
    Greater_China,
    South_East_Asia,
    Africa,
    Middle_East_and_North_Africa,
    Australasia,
    Indian_Peninsula,
    Japan_Korea,
    South_America,
    Central_America,
    Caribbean,
    Other
   );

  type Peril is
   (Drought,
    Earthquake,
    Flood,
    Frost,
    Hail,
    Windstorm
   );

  Peril_abbr : constant array (Peril) of String (1 .. 2) :=
   (Drought    => "DT",
    Earthquake => "EQ",
    Flood      => "FD",
    Frost      => "FT",
    Hail       => "HL",
    Windstorm  => "WS"
   );

  groupcountries_to_continent : constant array (GroupCountries) of Continent :=
   (France_Benelux => Europe,
    Northern_Europe => Europe,
    Central_and_Eastern_Europe => Europe,
    Southern_Europe => Europe,
    US => North_America,
    Canada_and_Greenland => North_America,
    Greater_China => Overseas,
    South_East_Asia => Overseas,
    Africa => Overseas,
    Middle_East_and_North_Africa => Overseas,
    Australasia => Overseas,
    Indian_Peninsula => Overseas,
    Japan_Korea => Overseas,
    South_America => Latin_America,
    Central_America => Latin_America,
    Caribbean => Latin_America,
    Other => Other
   );

  procedure Output_results (
    g       : GroupCountries;
    p       : Peril;
    to_file : String
  ) is
    use Ada.Text_IO;
    f : File_Type;
    separator : constant Character := ';';
  begin
    Create (f, Out_File, to_file);
    Put_Line (f, "Region: " & To_Lower (GroupCountries'Image (g)));
    Put_Line (f, "Continent: " & To_Lower (Continent'Image (groupcountries_to_continent (g))));
    Put_Line (f, "Peril type: " & To_Lower (Peril'Image (p)));
    New_Line (f);
    for i in 1 .. 100 loop
      Put (f, Integer'Image (i) & ':' & separator);
      for j in 1 .. 50 loop
        Put (f, Integer'Image (i * j) & separator);
      end loop;
      New_Line (f);
    end loop;
    Close (f);
  end Output_results;

  procedure Pack_results (
    g            : GroupCountries;
    p            : Peril;
    to_archive   : in out Zip_Create_Info
  )
  is
    temp_name : constant String := "temp.csv";
    final_name : constant String :=
      To_Lower (
        Peril'Image (p) & '/' &
        Continent'Image (groupcountries_to_continent (g)) & '/' &
        GroupCountries'Image (g) &
        '_' & Peril_abbr (p) & ".csv"
      );
  begin
    Output_results (g, p, temp_name);
    --  Alternatives:
    --    1) use a string instead of a temporary file
    --    2) use an input stream
    --    3) instead of a .csv, an Excel file from Excel Writer
    --         ( http://excel-writer.sf.net/ ) as a string or a stream.
    Zip.Create.Add_File (
      Info              => to_archive,
      File_Name         => temp_name,
      Name_in_archive   => final_name,
      Delete_file_after => True
    );
  end Pack_results;

  procedure Output_all_results is
    MyStream_file : aliased Zip_File_Stream;
    archive : Zip_Create_Info;
  begin
    Create_Archive (archive,
      MyStream_file'Unchecked_Access,
      "detailed_results.zip"
    );
    for p in Peril loop
      for g in GroupCountries loop
        Pack_results (g, p, archive);
      end loop;
    end loop;
    Finish (archive);
  end Output_all_results;

begin
  Output_all_results;
end Demo_csv_into_zip;