SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

zip - как реализовать выгрузку?



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP
View previous topic :: View next topic  
Author Message
Crystal_Ra
Старший специалист
Старший специалист



Joined: 12 Mar 2008
Posts: 108
Location: Белгород

PostPosted: Sat Dec 27, 2008 8:46 pm    Post subject: zip - как реализовать выгрузку? Reply with quote

Суть проблемы такова: есть несколько файлов ( например TXT и DBF ) которые являются результатом работы программы и вот их хотелось бы иметь в одном пакете. При чем квалификация пользователей ниже плинтуса.. первое что пришло в голову это закатать все в zip архив ... или как-то так...
Возможно есть варианты?

Как работать с zip файлами в ABAP ?.

P.S. если нет реализаций стандартных, может подкинете ссылочку на формат файла zip? именно файла а не алгоритм компрессии?

_________________
(SAP) Система нипель... выпускает лучше, чем впускает!

Crystal_Ra
Back to top
View user's profile Send private message
mode
Участник
Участник


Age: 39
Joined: 29 Dec 2008
Posts: 1

PostPosted: Mon Dec 29, 2008 10:41 am    Post subject: Reply with quote

Использовать класс CL_ABAP_ZIP
но его нету в старых до(SAP 6.40)системах
тогда можно создать два инклуда
метод add добавляет файл в zip, save возвращает xString для сохранения

1. include Z_MSDOS.

Code:
class msdos definition final.
  public section.
    class-methods:   to_date importing       date type d returning value(msdos_date) type i.
    class-methods:   to_time importing       time type t returning value(msdos_time) type i.
    class-methods: from_date importing msdos_date type i returning value(date)       type d.
    class-methods: from_time importing msdos_time type i returning value(time)       type t.
endclass.

class msdos implementation.
  method from_date. " IMPORTING msdos_date TYPE i RETURNING value(date) TYPE d

*   MS-DOS format for date:
*     Bits 15:9 = year - 1980
*     Bits 8:5 = month of year
*     Bits 4:0 = day of month

    constants: mfe00(2) type x value 'FE00',
               m01e0(2) type x value '01E0',
               m001f(2) type x value '001F'.

    data: x(2)  type x,
          year  type i,
          month type i,
          day   type i,
          c4(4) type c,
          str   type string.

*   Bits 15:9 = year - 1980
    x     = msdos_date.
    x     = x bit-and mfe00.
    x     = x div 512. " >> 9
    x     = x bit-and m001f.
    year  = x.
    year  = year + 1980.
    write year to c4 using edit mask 'RR____'.
    concatenate str c4 into str.

*     Bits 8:5 = month of year
    x     = msdos_date.
    x     = x bit-and m01e0.
    x     = x div 32. " >> 5
    x     = x bit-and m001f.
    month = x.
    write month to c4 using edit mask 'RR__'.
    concatenate str c4 into str.

*     Bits 4:0 = day of month
    x     = msdos_date.
    x     = x bit-and m001f.
    day   = x.
    write day to c4 using edit mask 'RR__'.
    concatenate str c4 into str.

*   Build date
    translate str using ' 0'.
    date = str.

  endmethod.

  method from_time. " IMPORTING msdos_time TYPE i RETURNING value(time) TYPE t.

*   MS-DOS format for time:
*     Bits 15:11 = hour   (24-hour clock)
*     Bits 10:5 = minute
*     Bits 4:0 = second/2

    constants: mf100(2) type x value 'F100',
               m07e0(2) type x value '07E0',
               m003f(2) type x value '003F',
               m001f(2) type x value '001F'.

    data: x(2)  type x,
          hour  type i,
          min   type i,
          c4(4) type c,
          str   type string.

*   Bits 15:11 = hour (24-hour clock)
    x     = msdos_time.
    x     = x bit-and mf100.
    x     = x div 2048. " >> 11
    x     = x bit-and m001f.
    hour  = x.
    write hour to c4 using edit mask 'RR__'.
    concatenate str c4 into str.

*   Bits 10:5 = minute
    x     = msdos_time.
    x     = x bit-and m07e0.
    x     = x div 32. " >> 5
    x     = x bit-and m003f.
    min   = x.
    write min to c4 using edit mask 'RR__'.
    concatenate str c4 into str.

*   Bits 4:0 = second/2
    concatenate str '00' into str.

*   Build time
    translate str using ' 0'.
    time = str.

  endmethod.

  method to_date. " IMPORTING date TYPE d RETURNING value(msdos_date) TYPE i.

*   MS-DOS format for date:
*     Bits 15:9 = year - 1980
*     Bits 8:5 = month of year
*     Bits 4:0 = day of month

    data: xdate(2) type x,
          x(2)     type x,
          year     type i,
          month    type i,
          day      type i.

*   Bits 15:9 = year - 1980
    year  = date+0(4).
    x     = year - 1980.
    x     = x * 512. " << 9
    xdate = xdate bit-or x.

*   Bits 8:5 = month of year
    month = date+4(2).
    x     = month.
    x     = x * 32. " << 5
    xdate = xdate bit-or x.

*   Bits 4:0 = day of month
    day   = date+6(2).
    x     = day.
    xdate = xdate bit-or x.

    msdos_date = xdate.

  endmethod.


  method to_time. " IMPORTING time TYPE t RETURNING value(msdos_time) TYPE i.

*   MS-DOS format for time:
*     Bits 15:11 = hour   (24-hour clock)
*     Bits 10:5 = minute
*     Bits 4:0 = second/2

    data: xtime(2) type x,
          x(2)     type x,
          hour     type i,
          min      type i,
          sec      type i.

*   Bits 15:11 = hour (24-hour clock)
    hour  = time+0(2).
    x     = hour.
    x     = x * 2048. " << 11
    xtime = xtime bit-or x.

*   Bits 10:5 = minute
    min   = time+2(2).
    x     = min.
    x     = x * 32. " << 5
    xtime = xtime bit-or x.

*   Bits 4:0 = seconds
    sec   = time+4(2).
    x     = sec / 2.
    xtime = xtime bit-or x.

    msdos_time = xtime.

  endmethod.
endclass.


2. include Z_CL_ABAP_ZIP.

Code:
*&---------------------------------------------------------------------*
*&       Class ZCL_ABAP_ZIP
*&---------------------------------------------------------------------*
*        Text
*----------------------------------------------------------------------*
class ZCL_ABAP_ZIP definition.

*"* public components of class CL_ABAP_ZIP
*"* do not include other source files here!!!
public section.
  type-pools ihttp .

  types:
    begin of t_file,
           name type string,
           date type d,
           time type t,
           size type i,
         end of t_file .
  types:
    t_files type table of t_file .
  types:
    begin of t_splice_entry,
           name       type string,
           offset     type i,
           length     type i,
           compressed type i,
         end of t_splice_entry .
  types:
    t_splice_entries type standard table of t_splice_entry with default key .

  data files type t_files read-only .

  methods add
    importing
      !name type string
      !content type xsequence .

  methods save
    returning
      value(zip) type xstring.

  class-methods crc32
    importing
      !content type xstring
    returning
      value(crc32) type i.


  private section.

  types:
    begin of t_ext,
           min_extract_version type i,
           gen_flags           type i,
           compressed          type i,
           compsize            type i,
           crc32(4)            type x,
           filename_len        type i,
           filename            type xstring,
           extra_len           type i,
           extra               type xstring,
           content             type xstring,
         end of t_ext .
  types:
    t_exts type table of t_ext .

  data exts type t_exts .
  class-data crc32_map type xstring.

endclass.               "ZCL_ABAP_ZIP
*&---------------------------------------------------------------------*
*&       Class (Implementation)  ZCL_ABAP_ZIP
*&---------------------------------------------------------------------*
*        Text
*----------------------------------------------------------------------*
class ZCL_ABAP_ZIP implementation.
**************************************************
method add.
  field-symbols: <file> type t_file,
                 <ext>  type t_ext.

  append initial line to files assigning <file>.
  append initial line to exts  assigning <ext>.

  <file>-name = name.
  <file>-date = sy-datum.
  <file>-time = sy-uzeit.
  <file>-size = xstrlen( content ).

   CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
    EXPORTING
      TEXT     = <file>-name
    Importing
      BUFFER = <ext>-filename.

  <ext>-filename_len        = strlen( <file>-name ).
  <ext>-extra_len           = 0.
  <ext>-extra               = ''.
  <ext>-min_extract_version = 20.
  <ext>-gen_flags           = 0.

  if <file>-size > 0.
    <ext>-compressed        = 8. " gzip Deflate
    <ext>-crc32             = crc32( content ).
  cl_abap_gzip=>compress_binary(
    exporting raw_in       = content
              raw_in_len   = <file>-size
    importing gzip_out     = <ext>-content
              gzip_out_len = <ext>-compsize ).
  else. " folder
    <ext>-compressed        = 0. " gzip Stored
    <ext>-crc32             = 0.
    <ext>-compsize          = 0.
  endif.
**************************************************
endmethod.

method save.

* Documentation from: http://www.pkware.com/company/standards/appnote/appnote.txt

  data: x2(2) type x, x4(4) type x.

  define writex4.    " write xstring
    x4 = &2.
    concatenate &1 x4 into &1 in byte mode.
  end-of-definition.

  define write2.  " write two bytes from integer
    x2 = &2.
    concatenate &1 x2+1(1) x2+0(1) into &1 in byte mode.
  end-of-definition.

  define write4.  " write four bytes from integer
    x4 = &2.
    concatenate &1 x4+3(1) x4+2(1) x4+1(1) x4+0(1) into &1 in byte mode.
  end-of-definition.

* Process all files. We write in parallel the zip and the central directory to use later
  data: msdos_date type i, msdos_time type i.
  field-symbols:  <file>  type t_file,
                  <ext>   type t_ext.
  data: dir type xstring, start_offset(4) type x.

  loop at files assigning <file>.
    read table exts index sy-tabix assigning <ext>.
    start_offset = xstrlen( zip ).

    msdos_time = msdos=>to_time( <file>-time ).
    msdos_date = msdos=>to_date( <file>-date ).

*   zip data stream
    writex4  zip '504B0304'.                  " local file header signature
    write2   zip <ext>-min_extract_version.   " version needed to extract = 2.0 - File is compressed using Deflate
    write2   zip <ext>-gen_flags.             " general purpose bit flag
    write2   zip <ext>-compressed.            " compression method: deflated
    write2   zip msdos_time.                  " last mod file time
    write2   zip msdos_date.                  " last mod file date
    write4   zip <ext>-crc32.                 " crc-32
    write4   zip <ext>-compsize.              " compressed size
    write4   zip <file>-size.                 " uncompressed size
    write2   zip <ext>-filename_len.          " file name length
    write2   zip <ext>-extra_len.             " extra field length

    concatenate zip <ext>-filename <ext>-extra <ext>-content into zip in byte mode.

*   central directory stream (which has a lare duplicate sequence of zip header)
    data: dup_offset type i. dup_offset = start_offset + 4.
    writex4  dir '504B0102'.                  " central file header signature
    write2   dir 19.                          " version made by (== pkzip 2.04g)
    concatenate dir zip+dup_offset(26) into dir in byte mode.  " part which matches exactly zip header
    write2   dir  0.                          " file comment length
    write2   dir  0.                          " disk number start
    write2   dir  0.                          " internal file attributes
    write4   dir  0.                          " external file attributes
    write4   dir  start_offset.               " relative offset of local header
    concatenate dir <ext>-filename  <ext>-extra into dir in byte mode.  " file name + extra info

  endloop.

* Write Central Directory
  data: lines_files type i.    lines_files = lines( files ).
  data: xstrlen_dir type i.    xstrlen_dir = xstrlen( dir ).
  data: offset_dir  type i.    offset_dir  = xstrlen( zip ).

  concatenate zip dir into zip in byte mode.
  writex4  zip '504B0506'.                    " End of central directory
  write2   zip  0.                            " number of this disk
  write2   zip  0.                            " number of the disk with the start of the central directory
  write2   zip  lines_files.                  " total number of entries in the central directory on this disk
  write2   zip  lines_files.                  " total number of entries in the central directory
  write4   zip  xstrlen_dir.                  " size of the central directory
  write4   zip  offset_dir.                   " offset of start of central directory
  write2   zip  0.                            " ZIP file comment length

endmethod.
**************************************************
method crc32.
* Do the calculations by hand. This is going to be slow. This is going to be a pain.
* What is a man to do?

  constants: magic_nr(4)  type x value 'EDB88320',
             mffffffff(4) type x value 'FFFFFFFF',
             m7fffffff(4) type x value '7FFFFFFF',
             m00ffffff(4) type x value '00FFFFFF',
             m000000ff(4) type x value '000000FF',
             m000000(3)   type x value '000000'.

  if xstrlen( crc32_map ) = 0.
    do 256 times.
      data: c(4) type x, low_bit(4) type x.
      c = sy-index - 1.
      do 8 times.
        low_bit = '00000001'. low_bit = c bit-and low_bit.   " c  & 1
        c = c div 2. c = c bit-and m7fffffff. " c >> 1 (top is zero, but in ABAP signed!)
        if low_bit is not initial.
          c = c bit-xor magic_nr.
        endif.
      enddo.
      concatenate crc32_map c into crc32_map in byte mode.
    enddo.
  endif.

  data: len type i, n type i. "#EC *
  data: crc(4) type x value mffffffff, x4(4) type x, idx(4) type x.

  len = xstrlen( content ).
  do len times.
    n = sy-index - 1.
    concatenate m000000 content+n(1) into idx in byte mode.
    idx = ( crc bit-xor idx ) bit-and m000000ff.
    idx = idx * 4.
    x4  = crc32_map+idx(4).
    crc = crc div 256. crc = crc bit-and m00ffffff. " c >> 8
    crc = x4 bit-xor crc.
  enddo.
  crc = crc bit-xor mffffffff.

  crc32 = crc.

endmethod.
**************************************************
endclass.               "ZCL_ABAP_ZIP
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ABAP All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.